Module 1:
Python Basics
In this module, you will learn the fundamentals and basic concepts of Python programming.
We will use text-based coding and the platform will be repl.it . The module starts with how
to communicate with the computer.
In this module, random and colorama library will be used. At the end of the module, you will
be able to write a simple code with variables, print output, take user input, use conditional
statements (if-else) and use loops (for loops).
Flow of Concepts
• Introduction to Python
• Input and output statements
• Variables
• Data types
• Operators
• Conditionals (Decision making statements)
• Loops
• Revision and Review
Chapter 1 of 3:
Communicate with Computer
Introduction to Python
Python is one of the most popular programming languages in the world today. It is easy to
read and has a friendly syntax, which makes it easy to learn and one of the reasons why it
has become so popular.
It was created in 1991 by Guido Van Rossum. The origin of its name is inspired by the British
comedy series ‘Monty Python’, and not the Python snake :)
Python is a general-purpose programming language. It is used in a wide variety of fields, like:
• Data Science
• Machine Learning
• Artificial Intelligence
• Web Development
• Software Development
Here are some famous programs and websites built using Python:
Chatbot
Do you know about chatbots? Have you come across any?
Alexa, Siri, Google Assistant are some popular examples of chatbots. Let's try a live one
now: https://www.hdfcbank.com/ has a chatbot EVA. You can send it a few messages and
see its responses.
The first thing we taught the computer was how to send messages to us. Computers call it
printing.
print ("Welcome to Python programming")
Next computers need to receive a message from us, which is done using the command
input. If we write the input command, the computer will wait for a reply from us.
input ("What's your name?")
To store what the user says, we use a variable.
Note: To make the code more readable for us and for others, programmers often leave
comments explaining parts of the code. But we need to tell Python not to treat this as code,
else it will give errors. We can do so by putting a # in front of the line (as in the above Repl,
with the comment on how variable names are case-sensitive).
Variables
In the above chatbot, we used a variable (called name) to store the user's name.
What is a variable?
What is the use of a variable?
You can think of a variable as a container. You can store different values in the same
container, like the variable name can store "Aman" or "Carlson" or "Maya".
Note: Variables are case-sensitive so print (Name) or print (NAME) will give an error.
Data types
The type of data a variable can hold is called its Data type. It may be integer, string, boolean
etc. (A variable of type Boolean can either be True or False)
Type Casting
We can convert data from one type to another it is called Type casting. Here are ways to cast
other data types into each other:
str() is to convert a data type to string. It is often used to print integers as in Figure 1 above.
int() is to convert any data type into integer. It is often used with the input function because
input returns a string. Eg :
Different format of print
There's a simpler method when printing multiple variables of different data types, using the
format function, denoted by print (f'...'). Eg:
print (f'Hi {name}. You are {x} years old.')
Let's print text colored
We need to import colorama module:
• from colorama import Fore, Back, Style
• Fore is for the foreground (text color)
• Back for background color of text
• Style can be used to make text DIM, bright, normal and to reset everything back.
Chapter 2 of 3:
Operators and Conditional statements
Operators
There are a couple of other operators: Mod (%) and Exponent (**)
Mod operator returns the Remainder left after division. Eg:
15%10 = 5 ; 8%4 = 0; 3%2 = ?
Exponent (**) - 5**2 means 5 to the power 2 i.e. 5*5. Eg:
5**2 = 25; 5**3 = 125; 3**2 = 9
String Operators
Operators are not just for integers. In Fact you have already used an operator for joining
strings. Remember when your greeting chatbot said:
print ("Hello" + name)
The variable name was a string with username, say "Maya" and it would print Hello Maya.
The operator '+' when used with strings, simply means join the 2 texts one after the other
into a single text. So, "Hello" + "Maya" will become "HelloMaya". If you want the output
"Hello Maya", with a space between Hello and Maya, simple add a space in the first text -
"Hello ", and then try "Hello " + "Maya" -> "Hello Maya".
"+" for integers vs strings
"35" + "36" (Both the texts are string data types)
35 + 36 (Both the text given by the user are integer data types)
In the first case, both the texts are string so, the output will be 3536. The computer will just
weave the symbols together. It's like weaving beads in a string to form a necklace.
In the second case, the computer knows the text is an integer, so it will add them up to give
71 as the output.
Conditional Statements
When greeting your school teacher how do you decide whether to say Good Morning?
Well, you would first check the time, and if it is before 12 noon you would say Good
Morning. Simple, isn't it! Python works exactly like this.
Conditional Statements
Decision making is critical to computer programming. There will be many situations when
you will be given two or more options and you will have to select an option based on the
given conditions.
• Then which greeting you will choose?
• Why did you choose this greeting?
• What decision did you make?
Comparison Operators
To check the condition we need to compare values. Comparison operators are used to
comparing two values. Here are a few examples:
if time < 12: print("Good Morning")
if weather != "cloudy": print ("No rains expected today!")
if age >= 18: print ("Allowed to drive with valid license")
In the above cases, we have only one condition but what If we have more than one
condition?
Logical Operators
To check a speeding violation, the traffic police would check if you have a valid license AND if
your car's speed was over the speeding limit, say 60 kph.
Here, we need to combine two conditions to make a decision.
if license=="valid" and speed<=60: print ("Let the driver go")
Another example- if time>=12 and
time<=18: print ("Good Afternoon")
Similarly, you can use the "or" operator. Can you guess what the following code does:
if first_letter=="s" or
first_letter=="S": pr
int ("The name begins with the letter s")
"=" vs "==" : Assigning value (=) vs Checking value (==)
"=" sign is used for assigning value to a variable
fruit_name= "apple" It means the value of variable fruit_name is apple
But if you write print(fruit_name== "apple") , It checks if fruit_name is apple or not. If
fruit_name is apple it displays True otherwise it displays False.
Remember: "=" sign is to assign a value and "==" is to check a condition
Chapter 3 of 3:
Introduction to Loops, For loops
Use loops to solve a problem whenever you spot a pattern that is repeating.
"for" Loop - Counting Loop
A ‘for loop’ is used to execute the same code repeatedly a given number of times. So the
next time if you are told to write something like "I will not chew gum in class" 100 times -
you can use loops and just say:
for i in range(100):
print ("I will not chew gum in class")
Now, suppose we want to print the first 10 natural numbers using a loop. A for loop has two
blocks, one is where we specify the count or how many times to execute, and then we have
the body where the statements are specified which get executed on each iteration.
Range specifies how the value of the Iterator (x) changes. So in the example here,
1. range (10) means value of x starts from 0,
2. and increases by 1 every time,
3. and goes till 10 - but not including 10, so it ends at 9
If we open the loop up, here is what the code would look like :
print (1) #1st Iteration (x=0)
print (2) #2nd Iteration (x=1)
...
print (9) #9th Iteration (x=9)
#End of the Loop
In short, the starting value of x will be 0, so the code prints 0 then it increments the value of
x. The loop checks whether the value of x is less than 10, if yes, then it goes inside the loop
to print the value of x (1), again increment the value of x by 1, again checks the value of x is
less than 10, the print value of x and so on till x becomes 9.
Range function
range (10,50,5) means:
• start from 10
• increase by 5 every time
• go till 50 (but not including 50)
If only one value is specified, like range(50) other values are taken as default values i.e. range
(0,50,1) .
Note: By default python’s print() function ends with a newline, but to make it end with
symbol of your choice - you can specify a parameter called ‘end’ as follows :
In this example, since we have used the end " ", Hello World prints in one line.
You can use any separator with end. Eg:
print ("Age", end=":")
print (13)
would print Age:13