Learn to Code via Tutorials on Repl.it!

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

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.

``````red = "\033[0;31m"
green = "\033[0;32m"
yellow = "\033[0;33m"
blue = "\033[0;34m"
magenta = "\033[0;35m"
cyan = "\033[0;36m"
white = "\033[0;37m"
bright_black = "\033[0;90m"
bright_red = "\033[0;91m"
bright_green = "\033[0;92m"
bright_yellow = "\033[0;93m"
bright_blue = "\033[0;94m"
bright_magenta = "\033[0;95m"
bright_cyan = "\033[0;96m"
bright_white = "\033[0;97m"``````

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.

``````underline = "\033[4m"
italic = "\033[3m"
darken = "\033[2m"
invisible='\033[08m'
reverse='\033[07m'
reset='\033[0m'``````

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

#### THE END

Hope you liked it :)

CodeLongAndPros (1538)

Import os is not what I’d use.

I’d print "\033[2J"

userSM (248)

@CodeLongAndPros now I learnt something new

YashmitBhaveris (11)

@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 (1538)

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

CodeLongAndPros (1538)

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

YashmitBhaveris (11)

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

userSM (248)

@CodeLongAndPros forget windows, wandows for life

ch1ck3n (154)

@userSM Forget wandows, Windoge for life

Alice688 (97)

Great tutorial, very useful!

Bookie0 (5061)

``````black = "\033[0;30m"
red = "\033[0;31m"
green = "\033[0;32m"
yellow = "\033[0;33m"
blue = "\033[0;34m"
magenta = "\033[0;35m"
cyan = "\033[0;36m"
white = "\033[0;37m"
bright_black = "\033[0;90m"
bright_red = "\033[0;91m"
bright_green = "\033[0;92m"
bright_yellow = "\033[0;93m"
bright_blue = "\033[0;94m"
bright_magenta = "\033[0;95m"
bright_cyan = "\033[0;96m"
bright_white = "\033[0;97m"``````
userSM (248)

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

TheForArkLD (745)

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

Ergopeoxy (1)

noice, pls make more list of useful commands.

AjayManath (2)

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

DungeonMaster00 (165)

Talk about more Python modules and have codeblocks as examples

ParthChawla (12)

I Love it, It is very useful

zakdakidd (5)

Nice i like this

qwertyuiop1111 (9)

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

YuAndy (72)

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

qwertyuiop1111 (9)

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 (0)

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 (248)

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

personnoname (0)

this is so useful, thanks

Smart0ne (670)

You can do

``from termcolor import colored``

and do

``print(colored("Hi!","red"))``

to print "Hi!" in red.

userSM (248)

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

hollimu (0)

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

tankerguy1917 (152)

The random.choice isn't working. plz help

userSM (248)

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

tankerguy1917 (152)

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

CodingWarrior (0)

@userSM Will you make another list

userSM (248)

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

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 (248)

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

Jakman (464)

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 (248)

@Jakman thanks but no thanks (no offense)

Jakman (464)

@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 (924)

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 (9)

Thanks for the help.

qwertyuiop1111 (9)

@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 (248)

@qwertyuiop1111 sure, just invite me to a private repl

qwertyuiop1111 (9)

@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 (248)

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

qwertyuiop1111 (9)

@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 (248)

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

qwertyuiop1111 (9)

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

qwertyuiop1111 (9)

@userSM Think right now is good?

qwertyuiop1111 (9)

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

Leroy01010 (398)

And i thought i was a decent python coder

userSM (248)

@Leroy01010 this stuff isn't really advanced

potatojs (816)

noiiice but i already know that :D

userSM (248)

@potatojs this is for beginners or those new to python

potatojs (816)

but nice this will help a lot of people
@userSM

[deleted]

@userSM Hey bro u still wanna join my team?

[deleted]