Learn Python Through Anime: Import In Python

Before proceeding to part 3, let’s get some filler articles just like filler episodes in Anime. If you haven’t checked previous tutorials, Do check it out: Part 1, Part 2.

Why use Python libraries?

Python libraries are pre-written by a group of programmers in order to reduce your time hard coding every feature. Python libraries contain many features that are able to perform different tasks. Many Python packages/libraries are pre-installed with Python that includes math, sys, os, random, string, and many more. Now, what if you need to install the other libraries/packages? Is there any method to do this? Sure there is. PIP also known as Python Package Index, is a sort of free app store for Python modules. PIP is used to install the third-party modules/libraries that are written by talented programmers. Most of them are open-source, programmers around the world contribute to it.

Note: Python modules, libraries, or packages are the same.

import random 

Random modules are used when you have to generate random values.


This is used to give a single integer in the specified range. Now say you need a random number between 1 to 10.

First, you need to import the module:

import random

Once you imported the module use the random attribute. The attribute is something that defines the property of an object. You will learn about attributes and objects in OOPs. 

Random is the module name and randint is the attribute name [Syntax: module_name.attribute_name] random.randint(1,10), now this is used to give you one random integer value.

Now in range(1,10) we have seen it prints from 1 to 9 and excludes 10, in randint(1,10), the random is generated from 1 to 10, where 10 is included. 

>>> import random
>>> random.randint(1,10)
7 >>> random.randint(1,10) 2 >>> random.randint(1,10) 8


Sequence now that is something you encountered the first time. Remember long back we discussed data types, where we worked on a string, integer, float, and boolean. We have so far seen only 4 data types, some more are waiting for us. You have already worked with the sequence data types. String and range come under sequence data type. The list also belongs to a sequence which is used widely when it comes to random. choice.

We have already imported a random module. Now, let’s use the choice attribute, random.choice(string_value)

>>> name = "Berserk"
>>> random.choice(name)
'r'>>> random.choice(name)
>>> random.choice(range(1,10)) 8 >>> random.choice(range(1,10)) 7 >>> random.choice(range(1,10)) 3
>>> haikyuu_team = ["Karasuno","Nekoma","Aoba Jousei","Date Tech","Inarizaki"]
>>> random.choice(haikyuu_team)
>>> random.choice(haikyuu_team)

The above example returns one random value from a specified sequence. In the last example, you have seen a big list of teams inside a single variable, that is List. We will discuss list in part 4.


In all the above attributes of random we have used a parameter/argument inside the parenthesis (). In random.random(), You no need to specify anything, it randomly generates a value in intervals between 0 and 1. 

>>> random.random()
>>> random.random()
>>> random.random()

Import string

Moving on to the next pre-installed package/module string. So far we have known String just as a data type but next you will see how great String really is. Since you know how to import a module, go ahead and type the first line of code.

>>>import string

Let’s go back to a flashback like in most Shounen Anime.

ASCII stands for American Standard Code of International. Now, what exactly does this ASCII do here? We have 26 alphabets, 0-9 numbers, many special symbols. Now, why did I mention this here? You go to college, you are assigned with an identity card a unique code that is only assigned to you. In the same way in the programming world, ASCII assigns certain codes to every symbol, alphabet, and number. 

For e.g., small letter a is assigned 97.

a=97, b=98, c=99 …. z=122

In the same case capital letter, A is lesser than a because A=65.

A=65,B=66,C=67 … Z=90

For numbers:

‘0’=48, ‘1’=49, ‘2’=50 … ‘9’=57

Notice in numbers the data type in a string. We have discussed this already anything enclosed within quotes is treated as a string, even if you provide an integer value. Well now that I am wondering I don’t have a good memory to remember this code assigned to every character, is there any way I can check the code.

ord() and chr()

ord(string) : Return the Unicode code point for a one-character string

chr(integer) : Return a Unicode string of one character with ordinal integer


>>> ord('Z')
>>> ord(0)
TypeError: ord() expected string of length 1, but int found
>>> ord('0')
>>> ord('9')57
>>> chr(99)
'c' >>> chr(189)
>>> chr(108)'l'
>>> chr(78)'N'

Now back to the fight, OOPs I mean import string. A Shounen fight becomes good when you know about some flashbacks. ASCII plays an important role in import string. 

1. string.ascii

Now ASCII here is an attribute. Now there are three main attributes under this ASCII concept. We already know a-z as higher ASCII than A-Z. 

You can generate all alphabets using this operation.

>>> string.ascii_letters
>>> string.ascii_lowercase   #only lower case [a-z]
>>> string.ascii_uppercase  #only upper case [A-Z]

string.ascii_letters executes all the letters, and the other two are self-understood, after all your a Python Programmer you should notice the other two and justify your answer.

2. string.digit

This is used to return a number string from 0 to 9. Remember it’s a string type and not an integer.

>>> string.digits

3. string.punctuation

This attribute is used to return all special symbols characters.

>>> string.punctuation

How To Print Just A Single Character In Python?

Most of you might have come from C or Java background. One concept which is missing in Python is the char data type. Anything that is enclosed within the single quotes is referred to as char data type and if it is enclosed within double quotes it is String. But in Python, anything enclosed within single or double quotes is treated as the string itself. So what if we need just one character to be executed.

Here we will also check that this program should accept only 1 character and no more than one character. 

Is this really important? Where can this be used?

In some games like Hangman where we only accept a single character. 

while True:
    char = input("Enter just a character: ")
    if len(char)==1:
         print("Good, this is valid")
Enter just a character: lelouch
Enter just a character: light
Enter just a character: senku
Enter just a character: L
Good, this is valid

#method 2, automatically take the first character:

>>> char = input("Enter a character:")[0]

Enter a character:manga
>>> print(char)

In addition, part 2 and 3 consists of ProTips and Hands-on practice. Follow us on Twitter to stay Tuned.

If you like our content, support us by Buying a Coffee.

Try 30DaysPython programming.