Python Basics
Python Basics
To try the programs, you need to download Python from python.org only.
During installation, opt for Custom installation, and set the installation path for
python as C:\Python38 and let it install in default location.
Online Python 5
Every gmail account as well as your school official account allows you work on
In colab, you can type/edit the Python program online, run the Python program,
add notes along the code, share it with the teacher and peer to work in
collaboration. All this will happen as a part of your Google cloud/drive and you
don't have to bother about saving your program, but don't forget to name each
colab notebook with a meaningful name for your later comeback and work on it.
Each colab notebook will allow you to create multiple Python codes.
Python - The Programming Language 6
Python stands out as the present generation Programming Language because of the
● It's easy to learn - the time needed to learn Python is shorter than for many
Python in an example of :
Ans:
a. A high level programming language
Quiz 12
Ans:
c. A program written in a high level programming language
Quiz 13
What is IDLE?
Ans:
a. It is an acronym that stands for Integrated development and
learning environment for Python
Literals - the data in itself 14
The type of each of the different data as given in the previous slide can be
determined by the command type() in Python as follows:
4 is an int
4.0 is a float
You may think that they are exactly the same, but Python sees them in a
completely different way.
It is also important to note that it is not only a decimal point which makes a
numerical value float. A float value can also be represented using the exponent
notation E. For example 4E8 is a float value representing 4 x 108
Strings 17
● Strings are used when you need to process text (like names of all kinds,
addresses, novels etc.), not numbers.
"This is a string"
'This is a string'
"It's Correct"
'I am "Monty Python"'
"I am 'Monty Python'"
Boolean values 18
>>> type(True)
<class 'bool'>
Converts the boolean literal True into its
>>> int(True) equivalent integer 1
1
>>> type(False)
<class 'bool'>
Converts the boolean literal False into its
>>> int(False) equivalent integer 0
0
Quiz 19
a. int
b. float
c. str
d. bool
Ans:
c. str
Quiz 20
a. str
b. float
c. int
d. bool
Arithmetic operators 21
Python uses the following arithmetic operators upon different type of data
1. Exponential ** 3**2 9
6.25 ** 0.5 2.5
2. Multiplication * 4 * 3 12
2.5 * 2 5.0
"Python" * 2 "PythonPython"
Arithmetic operators 22
1 () 5 + - (Binary Operators)
3 + - (Unary Operators) 7 == !=
4 * / % // 8 = %= /= //= -= += *= **=
Evaluation of arithmetical expression 24
2 + 3 * 5 17
2 1
(2 + 3) * 5 25
1 2
2 - 3 * 5 + 4 / 2 -11.0 Note: 4/2 = 2.0
3 1 4 2 9/3 = 3.0
2 + 3 * (5 - 4) / 2 3.5
4 2 1 3
Interesting Facts of Arithmetic Operations 25
While all arithmetic operators are left bound, the exponential operator ** is always
>>> 2 ** 3 ** 2
2 1 >>>2**(3**2)
512
512
>>>(2**3)**2
64
>>> 16 % 5 % 3
1 1 2 >>>(16 % 5) % 3
1
>>>16 % (5 % 3)
0
Interesting Facts of Arithmetic Operations 26
● A division operator / always gives the result of division in float i.e. with a
>>> 7 / 2 >>> -7 // 2
We have seen that arithmetic operations upon data gives a result. However, if we
Python provides containers to store such values. These containers are called
variables and as the name suggests, the value stored in such a container can be
varied or altered at any point of execution.
You're allowed to use as many variable declarations as you need to achieve your
However, you are not allowed to use a variable which has not been created i.e.
The reason of the error above is that we tried to find type of variable age, though
the variable which we had created was Age.
Using Variables 36
A variable in Python mutates itself according to the data assigned to it at any point
>>> Value="Welcome"
>>> type(Value)
<class 'str'>
>>> Value = 25
>>> type(Value)
<class 'int'>
>>> Value = 4/2
>>> type(Value)
Can you guess why the type of
<class 'float'>
Value is float here ?
Using Variables 37
The value stored in a variable can be altered at any point of execution, using
Find the outputs of the following code for the red texted commands:
Comments in Python are used to describe what a particular segment of the code is
If a comment extends over multiple lines we can use triple quotes, either '''
'Ram Nagar'
NONE
Content allowed to be
changed
Numbers and Arithmetic Expressions 43
A=20 A=20
A=20 A=20
B=45 -25 <class 'int'> B=40 2 <class 'int'>
C=A-B C=B//A
print(C,type(C)) print(C,type(C))
A=20 A=2
B=45 900 <class 'int'> B=3 8 <class 'int'>
C=A*B C=A**B
print(C,type(C)) print(C,type(C))
Data Types in expressions and results 44
**
**
Python Relational Operators are used to compare two values and return a result
as either True or False:
Python Logical Operators are used to operate upon Boolean Values and return a
Assignment and Augmented (Arithmetic Assignment) operators are used to assign values to variables:
Assignment and Augmented (Arithmetic Assignment) operators are used to assign values to variables:
Instructions that a Python interpreter can execute are called statements. For example:
Python
print("Hello\nFriends") Hello
code
print('We aren\'t afraid') Friends
print("Just \t tabbed") We aren't afraid
print("My Car __/===\\__") Just tabbed
Output My Car __/===\__
print(" 'O O'")
'O O'
print() - Displaying outputs 54
The print() function prints the specified message to the screen. The message can
Parameter Description
object(s) Any object (variable/literal/calculation). If there are
multiple objects, they may be separated by comma.
Programmers often need to interact with users to get data for processing. Python
Syntax:
<Var>=input( <prompt> )
Parameter Description
prompt A String, representing a default message before the input.
Taking user input 59
English:65
for user Inputs as Maths:85
65 and 85
Average Marks: 75.0
Output →
Taking user input 63
Principal:7000
for user Inputs as Rate:10
7000,10 and 5
Time:5
Output →
Simple Interest: 3500.0
While conversion, keep the following in mind 64
Principal:7000
Rate:10
Time:Good Time
----------------------------------------------
ValueError: could not convert string to float: 'Good Time'
Simplicity of Instructions 65
You do not write programs for executing it once by the computer, you need to
It is very common for programmers not to understand the logic of their own
programs after some time. Maintenance and Modification of such programs
would be very difficult.
A=float(input("A:")) P=float(input("P:"))
B=float(input("B:")) R=float(input("R:"))
C=float(input("C:")) T=float(input("T:"))
D=A*B*C/100 SI=P*R*T/100
print(D) print(SI)
Simplicity of Instructions 66
Writing simple instructions helps in avoiding this problem. Some Tips are as
S=float(input("Sal:"))
IH=S+S*(30/100)+S*(10/100)
print(IH) Sal=float(input("Sal:"))
DA=Sal*(30/100)
Tax=Sal*(10/100)
InHand=Sal+DA-Tax
print(InHand)
Clarity & Simplification of expression 67
Keep the names of the variables simple, short and yet meaningful
Errors in a program 68
● Syntax Error: Syntax error, also known as parsing error, is the most common
kind of error in the program, which occurs due to wrong use of syntax of the
language. The process of translation of the script to be understood by the
system is known as parsing. The parser stops translation at the offending line
(line with wrong syntax) and displays a little ‘arrow’ pointing at the earliest
point in the line where the error was detected.
Syntax Errors - Examples 69
These errors occur due to wrong use of formula or expression by the programmer.
Correct Code after removal of Logical Error Correct Code after removal of Logical
Avg=(English + Maths + Science)/3 Error
Area = Length * Breadth
Run-time Errors/Exceptions 71
A=int(input("Numerator"))
B=int(input("Denominator"))
C=A/B;
print(C)
The program shown above will The program shown above will throw an
throw an Exception Exception (ValueError), if user enters a
(ZeroDivisionError), if user non numeric value for example Hello for
enters 0 for the variable B. any of the variables A or B.
72