L4-L5. Element of Python
L4-L5. Element of Python
Lecture 4-5
Elements of Python
Dr. Yadvendra Pratap Singh
Course Details Asst. Professor(SS)
(B. Tech. III Sem) CSE
The students will study the elements of Python like Keywords and
Identifiers, Variables, Data types and Operators, Comments.
• Every value in Python has a datatype, that are used to define the
operations possible on them and the storage method for each of
them.
• Since everything is an object in Python programming, data types are
classes and variables are instance (object) of these classes.
• Numbers represent numeric values. Number objects are created when a value
is assigned to them. For ex:
>>> var1 = 1
>>> var2 = 10
• The reference to a number object can be deleted by using the del statement.
The syntax of the del statement is
>>> del var # Deletes variable var
>>> del var1,var2,var3 # Deletes the variables var1, var2, var3
• A single object or multiple objects can be deleted by using the del statement.
For example
>>> del var
>>> del var_a, var_b
03/12/2024 Object Oriented Programming Unit I 13
Types of Number Literals(CO1)
• Appending is the joining the another string object on the end of first
string object.
>>> s1 = “Hello”
>>> s2 = “Students”
>>> s1 = s1 + s2
>>> s1
“HelloStudents”
[Note: New memory location is created for s1 after appending with s2.]
• For example
>>> str = ‘Hello World!’
>>> print(str) # Prints complete string
>>> print(str[0]) # Prints first character of the string
>>> print(str[2:5]) # Prints characters starting from 3rd to 5th
>>> print(str[2:]) # Prints string starting from 3rd character
>>> print(str * 2) # Prints string two times
>>> print(str + ‘TEST’) # Prints concatenated string
Slicing
• A range of characters can be returned by using the slice syntax.
• Specify the start index and the end index, separated by a colon, to
return a part of the string.
Example
Get the characters from position 2 to position 5 (not included):
Program
b = "Hello, World!"
print(b[2:5])
Program Output
llo
03/12/2024 Object Oriented Programming Unit I 23
Program to demonstrate slicing of Strings (CO1)
# String slicing
String ='ASTRING'
# Using slice constructor
s1 = slice(3)
s2 = slice(1, 5, 2)
s3 = slice(-1, -12, -2)
print("String slicing")
print(String[s1])
print(String[s2])
print(String[s3])
03/12/2024 Object Oriented Programming Unit I 24
Output of slicing of Strings (CO1)
Output:
String slicing
AST
SR
GITA
print("a is", a)
print("b is", b)
print("c:", c)
print("d:", d)
print(even_number)
print(odd_number)
print(alphabets)
print(information)
print(vowels)
print(fruits)
print(vowels)
print(fruits)
• The process of converting the value of one data type (integer, string,
float, etc.) to another data type is called type conversion.
• Python has two types of type conversion.
• Implicit Type Conversion
• Explicit Type Conversion
a//b = floor(a/b)
• When a and b are of same sign. Quotient is of +ve sign
• Example:
12/5 = -12/-5 = 2.4
12//5 = -12//-5 = floor(2.4) = 2
• When a and b are of opposite sign. Quotient is of -ve sign
• Example:
12/(-5) = (-12)/5 = -2.4
12//(-5) = (-12)//5 = floor(-2.4) = -3
a%b = a – b*floor(a/b)
• When a and b are of same sign.
• Example:
12%5 = 12 - 5*floor(12/5) = 12 – 5*2 = 2
-12%-5 = -12 – (-5)*floor(-12/-5)=-12-(-5)*(2) = -2
• When a and b are of opposite sign.
• Example:
12%(-5) = 12 –(-5)*floor(12/(-5)) = 12-(-5)*(-3) = -3
-12%5 = -12 – 5*floor(-12/5) = -12-5*(-3) = 3
Operator Description
a b a and b a or b not a
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
is not True if the operands are not identical (do not a is not b False
refer to the same object)
end='end' Optional. Specify what to print at the end. Default is '\n' (line
feed)
file Optional. An object with a write method. Default is sys.stdout