Python Basics
Python Basics
Python Basics
Python Keywords
Python keywords are reserved words that have a special meaning associated with them and can’t be used
for anything but those specific purposes.
Except for three keywords (True, False, None), all keywords have lower case letters.
import keyword
print(keyword.kwlist)
How to Identify Python Keywords
• Python keyword module allows a Python program to determine if a string is a keyword
or not.
• iskeyword(s): Returns True if s is a keyword
import keyword
print(keyword.iskeyword('if')) # True
print(keyword.iskeyword('range')) # False
Python Data Types
• Data types specify the different sizes and values that can be stored in the variable. For
example, Python stores numbers, strings, and a list of values using different data
types.
int data type
•Python uses the int data type to represent whole integer values.
•You can store positive and negative integer numbers of any length such as 235, -758,
235689741.
•We can create an integer variable using the two ways
•Directly assigning an integer value to a variable
•Using a int() class
# store int value
roll_no = 33
# display roll no
print("Roll number is:", roll_no) # output 33
print(type(roll_no)) # output class 'int’
cn = complex(5,6)
print(‘Given Complex Number:’,cn)
print('Real part is :',cn.real)
print('Imaginary part is :',cn.imag)
Output:
Given Complex Number: (5+6j)
Real part is: 5.0
Imaginary part is: 6.0
str data type
• In Python, A string is a sequence of characters enclosed within a single quote or
double quote.
• These characters could be anything like letters, numbers, or special symbols enclosed
within double quotation marks. “Computing”
• The string is immutable, i.e., it cannot be changed once defined. You need to create a
copy of it if you want to modify it. This non-changeable behavior is called immutability
platform = " Computing"
print(type(platform)) # <class 'str'>
# display string
print(platform) #'Computing'
# accessing 3rd character of a string
print(platform[3]) # p
Python Operators
• Operators are special symbols that perform specific operations on one or more
operands (values) and then return a result.
• For example, you can calculate the sum of two numbers using an addition (+) operator.
Python has seven types of operators that we can use to perform different operation
and produce a result.
o Arithmetic operator
o Relational operators
o Logical operators
o Assignment operators
o Bitwise operators
o Membership operators
o Identity operators
Arithmetic operator
/ vs // — division vs floor division
// : This operator will divide the first argument by the second and round the result down to
the nearest whole number.
The result of regular division (using the / operator) is , but using // has floored 3.75 down
to 3. print("Regular division")
print(2/2,"\t is a float.")
print("\nFloor division")
print(15 // 4, "\t is an int.")
print(15.0 // 4, "\t is a float.")
OUTPUT:
Regular division
1.0 is a float.
Floor division
3 is an int.
3.0 is a float.
Floor division with negative numbers
When an operand is negative, floor division will return the largest integer less than or
equal to the result of regular division.
Let's use the same operands as before to show how this works:
print(15 / 4)
print(15 // 4)
print(-15 // 4)
OUTPUT:
3.75
3
-4 ---->
Relational Operators
• Relational operators are used to compare the value of operands (expressions) to produce a logical value.
• A logical value is either True or False
Logical Operators
• Logical operators are used to connect two or more relational operations to form a complex expression called logical
expression.
• A logical value is either True or False
Assignment Operators
The operators which are used for assigning values to variables. ‘=’ is the assignment operator.
Assignment operators are used to perform arithmetic operations while assigning a value to a variable.
Bitwise Operators
Bitwise operators are used to perform operations at binary digit level.
Membership Operators
The membership operators are useful to test for membership in a sequence such as string, lists, tuples and
dictionaries.
There are two type of Membership operator:-
in
not in
in Operators
This operators is used to find an element in the specified sequence.
It returns True if element is found in the specified sequence else it returns False.
Examples:-
st1 = “Welcome to Python Class”
“to” in st1 #output: True
st3 = “Welcome to Python Class”
“java” in st3 #output: False
not in Operators
This operator works in reverse manner for in operator.
It returns True if element is not found in the specified sequence and if element is found, then
it returns False.
Examples:-
st1 = “Welcome to Python Class”
“java” not in st1 #output: True
st2 = “Welcome to Python Class”
“come” not in st2 #output: False
st3 = “Welcome to Python Class”
“to” not in st3 #output: False
Identity Operators
This operator compares the memory location(address) to two elements or variables or objects.
With these operators, we will be able to know whether the two objects are pointing to the same location or
not.
There are two identity operators in python,
o is
o is not.
is:
A is B returns True, if both A and B are pointing to the same address.
A is B returns False, if both A and B are not pointing to the same address.
is not:
• A is not B returns True, if both A and B are not pointing to the same object.
• A is not B returns False, if both A and B are pointing to the same object.
Example:
a = 25
b = 25
print(id(a)) #output: 10105856
print(id(b)) #output: 10105856
print(a is b) #output: True
print(a is not b) #output: False
Python Operators Precedence
Precedence level Operator Meaning
1 (Highest) () Parenthesis
2 ** Exponent
3 +x, -x, ~x Unary plus, Unary Minus, Bitwise negation
4 *, /, //, % Multiplication, Division, Floor division, Modulus
5 +, - Addition, Subtraction
6 <<, >> Bitwise shift operator
7 & Bitwise AND
8 ^ Bitwise XOR
9 | Bitwise OR
10 ==, !=, >, >=, <, <= Comparison
11 is, is not, in, not in Identity, Membership
12 not Logical NOT
13 and Logical AND
14 (Lowest) or Logical OR
Expressions in Python
A combination of values, variables and operators is known as expression.
Examples:
x=5
y=x+10
z= x-y*3
The Python interpreter evaluates simple expressions and gives results even without print().
For example,
>>> 5
5 #displayed as it is
>>> 1+2
3 #displayed the sum
>>> (10 - 4) * 2 +(10+2)
24
What is the output of the following:
i) a = 21
b = 13
c = 40
d = 37
p = (a - b) <= (c + d)
print(p)
Output: True
What is the output of the following:
i) a, b = 10, 20
print(a == b or a < b and a != b)
Output: True
Output: 116
What is the output of the following:
x = 10
y = 50
res= x ** 2 > 100 and y < 100
print(x, y, res)
Output: 10 50 False
For each of the following expressions, what value will the expression give?
Verify your answers by typing the expressions into Python.
1) 9 – 3
2) 8 * 2.5
3) 9 / 2 // : This operator will divide the first argument by the second
4) 9 / -2 and round the result down to the nearest whole number
5) 9 // -2
6) 9 % 2
7) 9.0 % 2
8) 9 % 2.0
9) 9 % -2
10)-9 % 2
11)9 / -2.0
12)4 + 3 * 5
13)(4 + 3) * 5
Type Conversion or Type Casting
In Python, we can convert one type of variable to another type. This conversion is called
type casting or type conversion.
Python performs the following two types of casting.
o Implicit casting: The Python interpreter automatically performs an implicit Type
conversion, which avoids loss of data.
o Explicit casting: The explicit type conversion is performed by the user using built-in
functions.
# Python program to demonstrate implicit type Casting
# Python automatically converts
a = 7
print(“type of a:”,type(a))
b = 3.0
print(“type of b:”,type(b))
b=7
q= float(b)
print(“value of b is :”,b)
print(“value of q is :”,q)
print("The type of 'b' is ",type(b))
print("The type of 'q' is",type(q))
#program to demonstrate explicit type conversion
a=10
print("value of a is :",a)
print("The type of 'a' is ",type(a))
p=str(a)
print("value of a is :",p)
print("The type of 'p' is",type(p))
b='8'
print("value of b is :",b)
print("The type of 'b' is ",type(b))
q=int(b)
print("value of q is :",q)
print("The type of 'q' is",type(q))
c='7.9'
print("value of c is :",c)
print("The type of 'c' is ",type(c))
r=float(c)
print("value of r is :",r)
print("The type of 'r' is",type(r))