Basics
Basics
CONTENTS
• Python Basics
– Comments
– Print
– User Input
– Variables
– Datatypes
– Operators
Python Basics-Comments
1. Comments are used for documenting the code .
2. Comment lines will not be executed by the python interpreter.
Multiline statements:
result=(2*5)+
Single Line Comment:
9
#This is a single line comment
result=(2+3)*(7+8)\
*(9+10)
Multi-line Comment:
result=(2*4*
“”” This is multiple ”””
4)
(Acceptable for all kinds of
brackets)
Python Basics-Print statement
To display output on the screen of the user
Python Basics-User Input statement
To get user input through keyboard
All inputs got from the user are treated as Strings
Python –Fundamental Components
Operators Identifiers
Variables Datatypes
Identifiers Variables
Case-Sensitive Eg : Bill_id
Cannot match keywords _billid1
Variables and its dimensions
Identify the
variable
Data held by
the variable
Datatypes
Built-in functions
For handling different types of user
inputs
int()-returns a integer number
float()-returns a decimal/floating point number
bool()-returns a Boolean value
complex()-returns a complex number
str()-returns a string
Simple Python codes
Operators
Arithmetic Operators
• An arithmetic operator is a mathematical operator that takes
two operands and performs a calculation on them. They are
used for simple arithmetic.
Arithmetic Operators
#Demo Program to test Arithmetic Operators
a=100
b=10
print ("The Sum = ",a+b)
print ("The Difference = ",a-b)
print ("The Product = ",a*b)
print ("The Quotient = ",a/b)
print ("The Remainder = ",a%30)
print ("The Exponent = ",a**2)
print ("The Floor Division =",a//30)
#Program End
Output:
The Sum = 110
The Difference = 90
The Product = 1000
The Quotient = 10.0
The Remainder = 10
The Exponent = 10000
The Floor Division = 3
Relational or Comparative operators
• A Relational operator is also called as Comparative operator
which checks the relationship between two operands. If the
relation is true, it returns True; otherwise it returns False
Relational Operators
#Demo Program to test Relational Operators
a=int (input("Enter a Value for A:"))
b=int (input("Enter a Value for B:"))
print ("A = ",a," and B = ",b)
print ("The a==b = ",a==b)
print ("The a > b = ",a>b)
print ("The a < b = ",a<b)
print ("The a >= b = ",a>=b)
print ("The a <= b = ",a<=0)
print ("The a != b = ",a!=b)
#Program End
Output:
Enter a Value for A:35
Enter a Value for B:56
A = 35 and B = 56
The a==b = False
The a > b = False
The a < b = True
The a >= b = False
The a <= b = False
The a != b = True
Logical operators
• In python, Logical operators are used to perform
logical operations on the given relational
expressions. There are three logical operators they
are and, or and not
Logical Operators
If A=(m>200), B=(m>300)
#Demo Program to test Logical Operators
a=int (input("Enter a Value for A:"))
b=int (input("Enter a Value for B:"))
print ("A = ",a, " and b = ",b)
print ("The a > b or a == b = ",a>b or a==b)
print ("The a > b and a == b = ",a>b and a==b)
print ("The not a > b = ",not a>b)
#Program End
Enter a Value for A:50
Enter a Value for B:40
A = 50 and b = 40
The a > b or a == b = True
The a > b and a == b = False
The not a > b = False
ASSIGNMENT OPERATORS
• In Python, = is a simple assignment operator to assign
values to variable. Let a = 5 and b = 10 assigns the value 5
to a and 10 to b these two assignment statement can
also be given as a,b=5,10 that assigns the value 5 and 10
on the right to the variables a and b respectively.
• There are various compound operators in Python like +=,
-=, *=, /=, %=, **= and //= are also available.
Assignment Operators
#Demo Program to test Assignment Operators
x=int (input("Type a Value for X : "))
print ("X = ",x)
print ("The x is =",x)
x+=20
print ("The x += 20 is =",x)
x-=5
print ("The x -= 5 is = ",x)
x*=5
print ("The x *= 5 is = ",x)
x/=2
print ("The x /= 2 is = ",x)
x%=3
print ("The x %= 3 is = ",x)
x**=2
print ("The x **= 2 is = ",x)
x//=3
print ("The x //= 3 is = ",x)
#Program End
Type a Value for X : 10
X = 10
The x is = 10
The x += 20 is = 30
The x -= 5 is = 25
The x *= 5 is = 125
The x /= 2 is = 62.5
The x %= 3 is = 2.5
The x **= 2 is = 6.25
The x //= 3 is = 2.0
Bitwise Operators
Shifts
We can also use a formula to find the answer, We can also use a formula to find the answer,
x<<y=x*2y x>>y=x/2y
Membership and Identity Operators
Coding Standards in python
Formatting output
• There are several ways to present the output of a program, data can be
printed in a human-readable form, or written to a file for future use.
Sometimes user often wants more control the formatting of output than
simply printing space-separated values. There are several ways to format
output.
• To use formatted string literals, begin a string with f or F before the opening quotation mark or
triple quotation mark.
Assume that mileage of the vehicle, amount per litre of fuel and distance for one way are given.