Skip to content
Sign upLog in
← Back to Community

The most useful python modules for beginners

Profile icon
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.

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')

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 :)
Comments, upvotes and feedback are appreciated :)

Voters
Profile icon
userSM
Profile icon
niralparmar
Profile icon
LiliumMaid
Profile icon
Spartan32123
Profile icon
KevinThomas10
Profile icon
angrydoge
Profile icon
Bumblebee99
Profile icon
glitchish
Profile icon
zakdakidd
Profile icon
SerenSmith
Comments
hotnewtop
Profile icon
CodeLongAndPros

Import os is not what I’d use.

I’d print "\033[2J"

Profile icon
userSM

@CodeLongAndPros
now I learnt something new

Profile icon
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()

Profile icon
CodeLongAndPros

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

Profile icon
Yoshiii

@CodeLongAndPros
huh?

Profile icon
CodeLongAndPros

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

Profile icon
Yoshiii

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

Profile icon
CodeLongAndPros

@YashmitBhaveris
But it won't clear the screen.

Profile icon
Yoshiii

@CodeLongAndPros
lemme try it brb!

Profile icon
Yoshiii

@YashmitBhaveris
it worked for me??

Profile icon
userSM

@CodeLongAndPros
forget windows, wandows for life

Profile icon
ch1ck3n

@userSM
Forget wandows, Windoge for life

Profile icon
CodeLongAndPros

@ch1cken
yes

Profile icon
Alice688

Great tutorial, very useful!

Profile icon
userSM

@Alice688
thanks a lot

Profile icon
Bookie0

You should add colors:

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"
Profile icon
userSM

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

Profile icon
TheForArkLD

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

Profile icon
Bookie0

great ;)

@userSM

Profile icon
Bookie0

ok

@TheForArkLD

Profile icon
Ergopeoxy

noice, pls make more list of useful commands.

Profile icon
userSM

@Ergopeoxy
sure

Profile icon
AjayManath

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

Profile icon
userSM

@AjayManath
thanks :)

Profile icon
DungeonMaster00

Talk about more Python modules and have codeblocks as examples

Profile icon
userSM

@nk1rwc
sure

Profile icon
abomazen

thanks ,its good

Profile icon
userSM

@abomazen
good to hear

Profile icon
ParthChawla

I Love it, It is very useful

Profile icon
userSM

@ParthChawla
thanks a lot :)

Profile icon
FounderRoomyfoo

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

Profile icon
FounderRoomyfoo

It Says That Random Is Not A Thing

Profile icon
[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.

Profile icon
Coder127263783

Really helpful

Profile icon
zakdakidd

Nice i like this

Profile icon
qwertyuiop1111

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

Profile icon
E-Sheep

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

Profile icon
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 "". Can somebody please help?

Profile icon
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!
Test

Profile icon
userSM

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

Profile icon
personnoname

this is so useful, thanks

Profile icon
MuhammedHamza

👍

Profile icon
Smart0ne

You can do

from termcolor import colored

and do

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

to print "Hi!" in red.

Profile icon
userSM

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

Profile icon
Smart0ne

@userSM
Yeah. :D

Profile icon
hollimu

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

Profile icon
tankerguy1917

The random.choice isn't working. plz help

Profile icon
userSM

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

Profile icon
tankerguy1917

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

Profile icon
userSM

@tankerguy1917
all hail python 😂

Profile icon
tankerguy1917

yes. all hail [email protected]

Profile icon
CodingWarrior

@userSM
Will you make another list

Profile icon
userSM

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

Profile icon
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.

Profile icon
userSM

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

Profile icon
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

Profile icon
userSM

@Jakman
thanks but no thanks (no offense)

Profile icon
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.

Profile icon
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