Learn to Code via Tutorials on Repl.it!

← Back to all posts
The most useful python modules for beginners
userSM

Not long ago, I was a beginner to python. I was eager to make games, but had a hard time finding how to create certain elements of the game, such as choosing a random number or shuffling a list. That is when I discovered that there was no special tutorial in most places to teach you how to do these simple things. So thus, a tutorial by me (obviously) to help in things that don't have special tutorials.
(These are things I found useful)

CHOOSING A RANDOM NUMBER

To do this, at the top of your code, type in import random. This is what you call a module. Basically, a module is a bunch of functions that you can import to use in your code.

For example, if you want to make a variable called x, and assign in to a random number, you would do the following:
x=random.randint(0,10)

This is assigning a random integer or number between 0 and 10 to x.
So to create a random integer, first import random, then randomvar=random.randint(start point, end point)

So what ever random number you want, just remember the random.randint() part.

CHOOSING A RANDOM ITEM FROM A LIST

To do this, instead of typing random.randint(), you type random.choice(thenameofyourlist).

For example, if I had a list like this:
fruit=['apple','banana','orange']

And I wanted to assign assign a variable called y to a random item from the fruit list, I would type the following:
y=random.choice(fruit)

SHUFFLING A LIST

If I wanted to shuffle fruit, just do the following:
random.shuffle(fruit)

WAITING A FEW SECONDS

For this, import time. Now if we want to wait between two print statements, we would do the following:
print('hi')
time.sleep(1)
print('bye')

In this case, it will print 'hi', wait 1 second, then print 'bye'.

PS- I made it wait only 1 second, but it can sleep for as long as you want by entering a different number in the time.sleep() function.

CLEARING THE TERMINAL

For this, import os then type os.system('clear'). This will clear the terminal.

COLOURS IN PYTHON

Thanks to @Bookie0 for reminding me to add colours
I wanted to to do this a lot when I started out with python, but I couldn't find anywhere that told me how to print coloured text. But after a little snooping around in other people's repls when posted, figured it out.

This is a list of colour values in python (copy and paste them into your code). To do an example, let's print('I am blue text') in blue (obviously). To print this in blue, it is similar to printing a variable with some text. We would do
print(Blue,'I am blue text')

We can use any colour or effect we want, from the above list.

Below are some additional effects and ways you can print your text (they all work in the way shown above.

Feel free to experiment with the colours and different types of text decoration.

THE END

Hope you liked it :)
Comments, upvotes and feedback are appreciated :)

Voters
LiliumMaid
Spartan32123
KevinThomas10
angrydoge
Bumblebee99
iocoder
zakdakidd
SerenSmith
SAMSIL
G92708
Comments
hotnewtop
CodeLongAndPros

Import os is not what I’d use.

I’d print "\033[2J"

userSM

@CodeLongAndPros now I learnt something new

Yoshiii

@a You guys should really use replit module.
First import it by entering import replit.
Then to clear the page you just do replit.clear()

CodeLongAndPros

@YashmitBhaveris That's not portable. Repl supports ANSI, it's a UNIX system.

Yoshiii
CodeLongAndPros

@YashmitBhaveris What if you run replit.clear() on your Wandows computer?

Yoshiii

@CodeLongAndPros Ohhhh I get, but if you import the module it should work?!?

CodeLongAndPros

@YashmitBhaveris But it won't clear the screen.

Yoshiii

@CodeLongAndPros lemme try it brb!

Yoshiii

@YashmitBhaveris it worked for me??

userSM

@CodeLongAndPros forget windows, wandows for life

ch1ck3n

@userSM Forget wandows, Windoge for life

CodeLongAndPros
Alice688

Great tutorial, very useful!

userSM

@Alice688 thanks a lot

Bookie0

You should add colors:

userSM

@Bookie0 yeah that's what I use in any of my python games. I'll add them :)

TheForArkLD

@Bookie0 Ahhhh it isn't incorrect but you can write it without write 0;

Bookie0

great ;) @userSM

Bookie0
Ergopeoxy

noice, pls make more list of useful commands.

userSM
AjayManath

woooow man nice nobody gives such tings beacuse they are minute ...i liked it yoo

userSM

@AjayManath thanks :)

DungeonMaster00

Talk about more Python modules and have codeblocks as examples

userSM

@nk1rwc sure

abomazen

thanks ,its good

userSM

@abomazen good to hear

ParthChawla

I Love it, It is very useful

userSM

@ParthChawla thanks a lot :)

FounderRoomyfoo

Still Got Problems with It Even Tho I Typed It The Same Way

FounderRoomyfoo

It Says That Random Is Not A Thing

[deleted]

The random choice one would be useful in my past visual novel, because the enemy could only attack, even though three actions were possible. If I create a list with the possible actions, it can be chosen randomly, making him either attack, attack strongly or guard, which were the three possible actions.
Alternatively, I could use randint and just code the actions the enemy could use as 1, 2 and 3, rather than making a list for the code to choose from... No idea why I didn't think about this before...
Either way, thanks for the tutorial. Much appreciated.

Coder127263783

Really helpful

zakdakidd

Nice i like this

qwertyuiop1111

A bit unrelated, know any good beginners to work with? Some people I've worked with know too much.

YuAndy

Great tutorial! I would recommend adding \x1b[38;2;R;G;Bm which I find very useful when coloring text based on RGB.

qwertyuiop1111

Hey! This is very useful, and I will admit I didn't read all of it (, because I want to get back to programming) but, I have a problem where for example I do a 'Whats your name' type of thing and I get the output "<built-in function input>". Can somebody please help?

MaliaKremer

Hello! I'm very new to python so let me know if this is wrong! I wanted to only use italic on only a select sentence or word in my print statements, but was having a hard time finding a way to make it not do the entire sentence or the back end of the statement when I tried to put it into the middle. It turns out I had to put something in that told the code there to end, and this was just changing the number before m to 0! I tried it out on most of the colors and all of the effects and it seemed to work! Feedback is appreciated!

userSM

@MaliaKremer well it works so it must be correct :D

personnoname

this is so useful, thanks

MuhammedHamza

👍

Smart0ne

You can do

and do

to print "Hi!" in red.

userSM

@Smart0ne but I think my method is faster. But then again, your method is more pythonic :)

Smart0ne

@userSM Yeah. :D

hollimu

Thank you. Just starting to learn, this has helped me understand somethings. Now to keep looking and learn more. Thanks again.

tankerguy1917

The random.choice isn't working. plz help

userSM

@tankerguy1917 the list name shouldn't be in quote marks

tankerguy1917

ooh. now i know things i didn't 2 minutes ago. thanks for the tutorial. and may python live long

userSM

@tankerguy1917 all hail python 😂

tankerguy1917

yes. all hail [email protected]

CodingWarrior

@userSM Will you make another list

userSM

@CodingWarrior Probably, but I need ideas on what to include

KadenReynolds

When I use import time and then put time.sleep(1) between print statements, it waits the 1 second then prints both prints at once, rather than printing, waiting, then printing.

userSM

@KadenReynolds on repl it it sometimes glitches, but works in the end

Jakman

you might want to include the sys library for creating command line argument applications. This makes it easy to automate processes. If you want me to tell you about it in a private repl then i will

userSM

@Jakman thanks but no thanks (no offense)

Jakman

@userSM its alright. But it is a very powerful part of python that few on this site wield. This skill can land you many jobs.

Codemonkey51

Few.... me knows a bit and just goes to the docs and learns, hey I didn't know getting a job was that ez @Jakman

qwertyuiop1111

Thanks for the help.

userSM

@qwertyuiop1111 your welcome:)

qwertyuiop1111

@userSM hey. It's been long since I said thanks, and I'm still not much better at Python. I'm having difficulty with the color thing. Can ya help me out? Thanks.

userSM

@qwertyuiop1111 sure, just invite me to a private repl

qwertyuiop1111

@userSM Alright. This isn't anything important so if you are on a rush I advise you not to join. Thanks for your time.

userSM

@qwertyuiop1111 I'm literally just doing random stuff in c#. You can invite me if you want 🙂

qwertyuiop1111

You have been invited. @userSM

qwertyuiop1111

@userSM I don't want to seem abusive, but, you are quite enjoyable to work with. Think you can go back on our project? Thanks.

userSM

@qwertyuiop1111 Sure! I just couldn't do it yesterday, feel free to invite me again.

qwertyuiop1111

@userSM Sure. Right now could do (if you can) its rather late over there.

qwertyuiop1111

@userSM Think right now is good?

qwertyuiop1111

@userSM About my Discord you might wanna resend the friend request. Its qwertyuiop#8145

Leroy01010

And i thought i was a decent python coder

userSM

@Leroy01010 this stuff isn't really advanced

Leroy01010
potatojs

noiiice but i already know that :D

userSM

@potatojs this is for beginners or those new to python

potatojs

yeah i'm kidding :)
@userSM

potatojs

but nice this will help a lot of people
@userSM

userSM

@potatojs thanks

[deleted]

@userSM Hey bro u still wanna join my team?

userSM

@FluidCycling yeah sure

[deleted]

@userSM I added u bro

userSM

@FluidCycling your not online

[deleted]

@userSM No way i just sent u

userSM

@FluidCycling so r u online

[deleted]
[deleted]

@userSM Go to all repls and serch for zoom meeting gen V2

k9chelsea2

this is actually extremley helpful lol even though i dont use python I still want to learn it so this is useful

userSM

@k9chelsea2 thanks a lot :)

k9chelsea2

np :3