Introduction to Python
Python curriculum
o Basic python
Introduction
Variables, Expression, Statements
Conditionals and Loops
Functions
File Handling
Modules
o Intermediate python
Class and Object
Inheritance
Exception handling
Iterators and Generators
List, Dictionary and Tuple
Sets and Commandline
o Advanced python
Multiprocessing
Multithreading
Decorators
Lambda
Regular Expressions
Context managers
What is python?
o An Interpreted, General Purpose (or Multipurpose), High level Programming with easy syntax
and dynamic semantics.
Dynamic semantics means variables are dynamic objects in Python.
C is statically typed language as once a variable is declared with a specific data type though out
the lifetime of the program the type of the variables remains unaltered.
A variable can be assigned any value at any time as data type specification is not there in Python.)
Note:
Interpreted: The program is executed (one instruction will be translated) by interpreter.
General purpose: Not restricted to any specific domain
Easy syntax and Dynamic semantics
int x, y; x = 20
x = 10; y = 20
y = 5; print(x + y)
printf("%d", (x + y)); x 20.5
y = 21.7
print(x + y)
C Program (Statically typed) Python code (Dynamically typed)
o Python is a hybrid programming language as it is compiled and then interpreted.
CSwithSKS 9830814843
How to see the bytecode?
o Write a python program
o import py_compile
o py_compile.compile('[Link]')
_pycache_\\[Link]
Why Python is so popular?
o Open source
o Huge community support
o Cross platform
o Huge set of libraries and tools
Environment set up
1. Python install: [Link]/downloads −> 3.11.2
2. Visual Studio Code/ PyCharm
A Python code can be executed in three different ways:
1. Interactive approach
2. Run a script by python interpreter
3. Use an IDE
Commenting in Python
# is used before a text to make it a comment.
Indentation in Python
Indentation is the leading whitespaces before any statement
Purpose:
o Enhances the readability
o used to indicate a block of the code in python
int x = 20; x = 20
if (x % 2 == 0) if x % 2 == 0:
{ print('The number is even')
printf("The number is even"); else:
} print('The number is odd')
else
{
printf("The number is odd");
}
C version Python version
How indentation works
name = 'SKS' name = 'SKS'
if name == 'SKS': if name == 'SKS':
print('Hello SKS') print('Hello SKS')
else: else:
print('Hello Brother') print('Hello Brother')
print('This is the end') print('This is the end')
CSwithSKS 9830814843
Rules for using indentation
1. Minimum one space is necessary to represent an indented statement
2. The first line of the python code cannot have indentation
3. Indentation is mandatory in python to define a block of code
4. The no of spaces must be uniform
Python Identifier
o An identifier can be created by the symbols {A, .., Z, a, ..., z, _, 0, .., 9}
o No keyword can be used
o Python a case-sensitive language i.e. RAM and ram are different identifiers in Python.
Declaring a variable
Variables need not to be declared prior to their use as we do in C, C++ etc. to reserve memory space.
When we initialize a variable in Python, Python interpreter will automatically does the declaration.
Python data types
Numeric
o Integers
o Real
String
List
Tuple
Dictionary
Boolean
Set
A. Integer type
o In python, there is no limit on the size of integers
o Decimal integer is default
o Non decimal integers can be represented by adding prefixes
0b or 0B = Binary
0o or 0O = Octal
0x or 0y = Hexadecimal
o Even if prefixes are used to non-decimal numbers when displayed in will be displayed in
Decimal
B. Float type
o 'float' is used to represent real numbers and written with a decimal point
o to specify mantissa e exponent form add e or E at the end of a real number
= 45.345 = 4.5345 × 10 = 4.5345 e +01
Exponent
Mantissa
= 0.000034 = 3.45 × 10 = 3.45 − 05
C. Boolean type
o used to represent either True or False
CSwithSKS 9830814843
x = 12 name = 'Sam'
y = 13 if name == 'SKS': #False
print(x>y) print("Hello SKS")
else
print("Hello Brother")
D. String type
o A string is a sequence of characters
o Either a single quote or a double quote is used as a string enclosing operator
E. List
o List is the most used data types in Python. A list can contain the same type of items.
o Alternatively, a list can also contain different type of items.
o A list is an ordered and index-able sequences.
o It is somewhat similar to the array in C language. However, an array can contain only the same
type of items while a list can contain different type of items also.
=[ , , , , ]
=[ , Ram, . , ]
print(list1)
print(list2)
F. Tuple
o A tuple is similar to a list, containing a sequence of element.
o Howveer, a list is enclosed within [] and a tuple is enclosed within (). List is mutable where tuple
is immutable. Tuples are read only lists. Once, the items are stored the tuple cannot be modified.
tuple1 = (10, 20, 30, 40, 50)
G. Dictionary
o It is similar to hash table type. The order of elements in a dictionary is undefined. We can iterate
over the following:
o A Python dictionary is an unordered collection of key-value pairs. Keys and values can be of any
type in a dictionary. Items in dictionary are enclosed in the curly-braces {} and separated by ,. A
colon is used to separate key from value. A key inside the square bracket [] is used for accessing
the dictionary items.
dictionary1 = {101: "Ram", 102: "Sam", 103: "Madhu"}
print(dictionary1)
Variables in Python
o A variable in Python is a placeholder having some name, attached to a value
o No need for explicit declaration of variable.
o The type of a variable need not be specified in advance and the variable can store any type of
value during its lifetime
Variable naming convention
1. Must use {a, b,..., z, A, B, ..., Z, 0, 1, .., 9, _} and must start with either an alphabet or an
underscore e.g. SKS → Valid; 0SKS → Invalid; SKS_09 → Valid; SKS 09 → Invalid
CSwithSKS 9830814843
2. Spaces are not allowed
3. Variables in Python are case sensitive
e.g. sks = "name"; SKS = "name"
4. For a multiword variable name either of the following conventions can be used
Camel Case Convention: Second and subsequent words must be capitalized
e.g. sumMarks; sumOfMarks
Pascal Case Convention: Each word in the variable name must be capitalized
e.g. SumOfMarks; NameOfStudent
Snake Case Convention: Words are separated by underscores
e.g. Sum_Marks; Sum_Of_Marks
Chained assignment of Variables
Python allows chained assignment of variables that helps in assigning same value within different
variables in the same line e.g.
x = y = 10
print(x)
10
print(y)
10
print(x+y+z)
30
Variables as labels in Python
int x; //A location in memory is x = 12
reserved for the variable x
print(x)
x = 12;
printf("%d", x); x 12
x = y = 12
variable in C x 12 y
variable in Python
Initialization of a variable
Compile time (within the code)
x = 12
y = 34.45
z = True
name = "SKS"
Run time (during the execution)
There are two built in functions in Python to read the value from the user -
o input()
o raw_input() Used in Older versions like 2.x
CSwithSKS 9830814843
1. input()
Takes the input from the user and then evaluates the expression
By default input() takes input as string. Therefore, if input has to be taken as integer or float type then
explicit type conversion is needed.
To read multiple values in one line we can use either of the following
o split()
o List comprehension
2. raw_input()
CSwithSKS 9830814843