PYTHON REVISION TOUR
Characteristics of Python:
Object-Oriented, Open-Source, Portable, Platform-independent,interpreted Language developed by Guido Van
Rossum in 1991.
Character Set of Python:
Set of all characters recognised by python. It includes:
• Letters: A- Z and a -z
• Digits: 0-9
• Special Symbols: + , - , /, % , ** , * , [ ], { }, #, $ etc.
• White spaces: Blank space, tabs, carriage return, newline, form feed
• Other Characters: All ASCII and UNICODE characters.
Tokens: Tokens are smallest identifiable units in a python program. Different tokens are:
● Keywords: reserved words which are having special meaning. Examples: int, print, input, for, while
● Identifiers: name given by programmer to identify variables, functions, constants, etc.
● Literals: literals are constant values.
● Operators: Operators trigger an operation. Parameters provided to the operators are called operands.
Examples: + - * % ** / //
● Delimiters: Symbols used as separators. Examples: {} , [ ] ; :
Rules for identifier names:
● Keywords cannot be used
● Can contain A-Z,a-z,0-9 and underscore(_)
● Cannot start with a number.
Example: engmark, _abc , mark1, mark2
Variables and data types: Variables are used to store data. Data types of variables:
● Numeric Types: int, float, complex
● Boolean – True or False values.
● None – a special type with an unidentified value or absence of value.
● Sequence: an ordered collection of elements. String, List and Tuples are sequences.
● Mappings – Dictionaries are mappings. Elements of dictionaries are key-value pairs.
Mutable and Immutable types:
Mutable -Values can be changed in place. E.g. List, Dictionary
Immutable: Values cannot be changed in place in memory. E.g., int, float, str, tuple.
Assigning Values to Variables:
The assignment operator (=) is used to assign values to variables. E.g. a = 100
Multiple Assignments:
• Assigning same value to multiple variables - a=b=c=1
• Different values to different variables- a, b, c = 5,10,20
print statement: used to display output. If variables are specified i.e., without quotes then value contained in variable is
displayed.
,E.g., x = "kve " Output:
print(x) kve
To combine both text and a str variable, Python uses the “+” character:Example
x = "awesome" Output:
print("Python is " + x) Python is awesome
Expressions: An expression is combination of values, variables and operators. E.g. – x= 3*3//5 Operators: Operators
trigger an operation. Some operators need one operand, some need more than one operand to perform the operation.
Types of operators:
• Arithmetic Operators: + - * / (division) // (Floor division) % (remainder) ** (power)
• Relational Operators > < >= <= = = (equality) != (inequality)
• Logical Operators: not and or
Precedence of Operators: The operator having high priority is evaluated first compared to an operator in lower order of
precedence when used in an expression.
Examples:
1. x = 7 + 3 * 2 2. >>> 3+4*2
13 11
* has higher precedence than +, so it first Multiplication gets evaluated before the
multiplies 3*2 and then adds into 7 addition operation
Comments: Comments are ignored by the Python interpreter. Comments gives some messageto the Programmer. It can
be used to document the code. Comments can be:
• Single-line comments: It begins with a hash(#) symbol and the whole line is consideredas a comment until
the end of line.
• Multi line comment is useful when we need to comment on many lines. In python, tripledouble quote (" " ")
and single quote (' ' ') are used for multi-line commenting.
Example:
# This is a single line comment' ' 'I am
a multi
line comment ' ' '
, input() function:An input() function is used to receive the input from the user through thekeyboard. Example:
x=input('Enter your name')
age = int( input ('Enter your age'))
Selection Statements: It helps us to execute some statements based on whether the conditionis evaluated to True or
False.
if statement: A decision is made based on the result of the comparison/condition. If condition isTrue then statement(s)
is executed otherwise not.
Syntax:
if <expression>:
statement(s)
Example:
a=3 Output:
if a > 2: 3 is greater
print(a, "is greater") done
print("done")
If-else statement:
If condition is True then if-block is executed otherwise else-block is executed.
Syntax:
if test expression:
Body of if stmts
else:
Body of else
Example:
a=int(input('enter the number')) Output:
if a>5: enter the number 2
print("a is greater") a is smaller than the input given
else:
print("a is smaller than the input given")
If-elif-else statement: The elif statement allows us to check multiple expressions for TRUEand execute a block of
code as soon as one of the conditions evaluates to TRUE.
Syntax:
If <test expression>:
Body of if stmts elif <
test expression>:
Body of elif stmtselse:
Body of else stmts
Example:
var = 100 Output:
if var == 200: 3 - Got a true expression value
print("1 - Got a true expression value") 100
print(var)
elif var == 150:
print("2 - Got a true expression value")
print(var)
, elif var == 100:
print("3 - Got a true expression value")
print(var)
else:
print("4 - Got a false expression value")
print(var)
Iteration/Repetition: Repeated execution of a set of statements is called iteration. Thefor-statement and while-
statements can be used
While loop: while loop keeps iterating a block of code defined inside it until the desiredcondition is met.
Syntax: Example: Output: KV
while(expression): i=1 SchoolKV
Statement(s) while i<=3: SchoolKV
print("KV School") School
i=i+1
For loop: for loop iterates over the items of lists, tuples, strings, the dictionaries and otheriterable objects.
Syntax:
for <loopvariable> in <sequence>:
Statement(s)
Example 1: Output is:
L = [ 1, 2, 3, 4, 5] 12345
for var in L:
print( var, end=’ ‘)
Example 2: output: 0,1,2,3,4,5,6,7,8,9
for k in range(10):
print(k, end =’ , ‘)
‘break’ statement: Terminates the loop upon execution
‘continue’ Statement: Skips the current iteration / rest of the and continues on with the nextiteration in the loop.
Strings: A string is a sequence of characters. E.g. 'banana', "apple", '''orange'''
String slices: A segment or a part of a string is called a slice.
Syntax: string_object[Start: stop: steps]
● Slicing will start from index and will go up to stop(excluding stop) in step of steps.
● steps default is 1. If negative step then sequence will be decreasing order
● Default value of start is 0 if step is positive and the last item if step is negative
Example:
str = 'Hello World!'print Output:
(str[2:5] ) llo
print (str[2:]) llo World!
String concatenation: ‘+’ is string concatenation operator.
String repetition operators: ‘*’ is called repetition operator