Python chapter 1 basics module
Python chapter 1 basics module
Python Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator
operates on is called the operand.
In this article, you'll learn everything about different types of operators in Python, their syntax and how to
use them with examples.
Arithmetic operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication
etc.
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
Example:
x = 15
y=4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
print('x ** y =',x**y)
Output:
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
Comparison operators
Comparison operators are used to compare values. It either returns True or False according to the
condition.
Example
x = 10
y = 12
# Output: x == y is False
print('x == y is',x==y)
# Output: x != y is True
print('x != y is',x!=y)
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
# Output: x >= y is False
print('x >= y is',x>=y)
Logical operators
Example
x = True
y = False
# Output: x or y is True
print('x or y is',x or y)
Assignment operators
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
There are various compound operators in Python like a += 5 that adds to the variable and later assigns the
same. It is equivalent to a = a + 5.
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
Special operators
Python language offers some special type of operators like the identity operator or the membership operator.
They are described below with examples.
Identity operators
is and is not are the identity operators in Python. They are used to check if two values (or variables) are
located on the same part of the memory. Two variables that are equal does not imply that they are identical.
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
Example:
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
Here, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same is the
case with x2 and y2 (strings).
But x3 and y3 are list. They are equal but not identical. It is because interpreter locates them separately in
memory although they are equal.
Membership operators
in and not in are the membership operators in Python. They are used to test whether a value or variable is
found in a sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
Example:
x = 'Hello world'
y = {1:'a',2:'b'}
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)
Here, 'H' is in x but 'hello' is not present in x (remember, Python is case sensitive). Similary, 1 is key and 'a' is
the value in dictionary y. Hence, 'a' in y returns False.
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
Flow Control
In this article we will learn about control statements that are available in Python language. It is very important
to control the program execution because in real scenarios the situations are full of conditions and if you want
your program to mimic the real world closer then you need to transform those real world situations into your
program. For this you need to control the execution of your program statements. This article is all about
controlling the program execution sequence. It is commonly known as control flow in programming terms.
So let’s dive in the river of program statements that is controlled by python control flow tools.
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
Control flow tools in python
Python provide various tools for flow control. Some of them are if , if .. elif .. else, if..else,while ,for , switch,
pass, range, break, else, continue, function etc. In this article I’ll be covering only if-else and loops.
This control statement indicate that if something happens then do this. It’s a good way of handling some short
conditions. An if block can be followed by zero or any number of else block.
if (condition):
statements…
Example
A=10
If(a>=12):
Print(“Big Number”)
If … else
It’s like if have money then spend else wait for salary. I hope it’s clear from the previous line what this
statement means. It’s like if the conditions of if block is evaluated to true then execute that block else execute
the else block. The else block is optional and one if can have any number of else blocks.
Syntax:
if (condition):
statements…
else:
default statements…
Example
A=10
If(a>=12):
Print(“Big Number”)
else:
Print(“Small Number”)
If … elif … else
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
It’s like checking multiple conditions. Let’s Take an example if pocketMoney greater then 90T then Okay
else if pocket money is equal to 50T and Less then 90T then its average else it’s not enough. Basically that
statement can replace switch statement. You can put any number of elif blocks after if and else block is
optional.
Syntax:
if (option1 condition):
option1 statements…
elif(option2 condition):
option2 statements…
elif(option3 condition):
option3 statements…
else:
default option statements…
Example:
number = 23
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
print('Congratulations, you guessed it.')
print('(but you do not win any prizes!)')
# New block ends here
elif guess < number:
# Another block
print('No, it is a little higher than that')
# You can do whatever you want in a block ...
else:
print('No, it is a little lower than that')
# you must have guessed > number to reach here
print('Done')
# This last statement is always executed,
# after the if statement is executed.
For statement
It is used for looping over a sequence. Python doesn’t supports old for loop or c-style for loop. In traditional
style for loop we have one variable which iterates over a sequence and we can change the value of sequence
and variable as well but in modern for loop we have iteration variable that iterates over a fixed sequence. We
can not change the sequence as well as iteration variable during iteration.
Syntax:
Example:
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
1
Continue is used for continuing to next iteration of loop without doing anything inside the loop.
Else is introduced in python and it is placed in loop without if. It will execute only if the loop is terminated
without break.
Note: there are two more else statement, one is for if that I already explained and other is with try. The
difference between try else block and loop else is try else block executes when no code is executed and loop
else executes when no break is executed.
Example
politicleParties=['AAP','Elephent','Hand','Rest']
electionYear=["2014","2009","2005","2001"]
countryStatus=["worst","developing","developed"]
corruptionStatus=["Max","Normal","Min"]
for party in politicleParties:
year=input()
if year in electionYear:
if year == "2014":
print("AAp Wins!")
print("Country status: "+countryStatus[2])
print("Corruption Status: "+corruptionStatus[2])
break
elif year == "2009":
print("Hand Won")
print("Country status: "+countryStatus[0])
print("Corruption Status: "+corruptionStatus[0])
continue
elif year == "2005":
print("Hand won!")
print("Country status: "+countryStatus[0])
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
print("Corruption Status: "+corruptionStatus[0])
elif year == "2001":
print("Rest Won!")
else:
print("Wrong year of election!")
else:
print("The above loop was just for demonstration purpose!")
While Loop
The while loop is one of the first loops that you'll probably encounter when you're starting to learn how to
program. It is arguably also one of the most intuitive ones to understand: if you think of the name of this loop,
you will quickly understand that the word "while" has got to do something with "interval" or a "period of
time". As you already know by now, the word "loop" refers to a piece of code that you execute repeatedly.
With all of this in mind, you can easily understand the following definition of the while loop:
A while loop is a programming concept that, when it's implemented, executes a piece of code over and over
again while a given condition still holds true.
The above definition also highlights the three components that you need to construct the while loop in Python:
Now that you know what you need to construct a while loop, all that is left to do now is to look at a real-life
example where the while loop is used before you start making exercises on your own! Consider the following
example:
The code example above is a very simple while loop: if you think about it, the three components about which
you read before are all present: the while keyword, followed by a condition that translates to either True or
False (number < 5) and a block of code that you want to execute repeatedly:
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.
print("Thank you")
number = number + 1
If you go into detail in the above code, you see that there is a variable number in which you store an integer
2. Since the value in number is smaller than 5, you print out "Thank you" and increase the value of number
with one. While the value in number stays smaller than 5, you continue to execute the two lines of code that
are contained within the while loop:
"Thank you"
"Thank you"
You print out "Thank you" two more times before the value of number is equal to 5 and the condition doesn't
evaluate to True any more. Because the condition now evaluates to False, you will exit the while loop and
continue your program if it contains any more code. In this case, there isn't any more code so your program
will stop.
The above example is a bit basic, you can also include conditionals, or, in other words, an if condition, to
make it even more customized. Take a look at the following example:
# Increment `number` by 1
number = number+1
Designed by Abdur Rahman Joy - MCSD, MCPC, MCSE, MCTS, OCJP, Sr. Technical Trainer for C#.net, JAVA,
Android/IOS/Windows Mobile Apps, SQL server, Azure, Oracle, SharePoint Development, AWS , CEH, KALI Linux,
Python, Software Testing, Graphics, Multimedia and Game Developer at Joy Infosys, Cell #: +880-1712587348,
email: [email protected]. Web URL: http://www.joyinfosys.com/me.