Python Revision Tour I
Python Revision Tour I
Python is a General Purpose high level Programming language used for developing application
softwares. Developed by Guido van Rossum in 1991.
Features of Python
• High Level
• Case Sensitive
• Interpreted
• Platform Independent
• Dynamic
• General Purpose
To write and run python programs we need Python Interpreter also called Python IDLE.
Execution Mode
• Interactive mode
• Script mode
Interactive Mode
Python Keywords
Example:- False, class, finally, is, return, None, continue, for, try, def, from, while, if, or etc…
Identifier
Identifiers are name used to identify a variable, function or any other entities in a programs.
• The name should begin with an alphabet or and underscore sign and can be followed by any
combination of charaters a-z, A-Z, 0-9 or underscore.
• It can be of any length but we should keep it simple, short and meaningful.
Variables
• It can be referred as an object or element that occupies memory space which can contain a value.
• In python assignment statement is used to create variable and assign values to it.
Data types
These are keywords which determine the type of data stored in a variable. Following table show data
types used in python:
Comments
Operators
These are special symbols used to perform specific operation on values. Different types of operator
supported in python are given below:
• Assignment operator
Example
Sequence
**
* / // %
+ -
Evaluation:
#step1
#step2
= 56 + 10 + 1-(2*3)
#step3
= (56+10) +1-6
#step4
= (66 + 1) - 6
#step5
= 67-6
#step6
= 61
Statement
Example:
Syntax:
Input([prompt])
Syntax:
print([message/value])
Example:
Example:
CHECKING CONDITIONS
if
a = 33
b = 200
if b > a:
print("b is greater than a")
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
if else
a = 200
b = 33
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
nested if else
You can have if statements inside if statements, this is called nested if statements.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a
string).
This is less like the for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
The range() function returns a sequence of numbers, starting from 0 by default, and increments
by 1 (by default), and ends at a specified number.
Nested Loops
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
for x in adj:
for y in fruits:
print(x, y)
Type Conversion
• Explicit conversion
• Implicit conversion
Explicit Conversion
Syntax:
(new_data_type) = (expression)
Example:
x = 12
y=5
print(int(x/y)) #output - 2
Program of explicit type conversion from string to int
x = input("enter a number")
x = int(input(Enter a number"))
Implicit conversion
• Implicit conversion allows conversion from smaller data type to wider size data type without any
loss of information
Example:
Strings
• Character in string can be any letter, digit, whitespace or any other symbol.
• String can be created by enclosing one or more characters in single, double or triple quotes.
Examples:
• Indexes are unique numbers assigned to each character in a string to identify them
Negative Index
• Negative indices are used when you want to access string in reverse order.
• Starting from the right side, the first character has the index as -1 and the last character (leftmost) has
the index -n where n is length of string.
Is string immutable?
• Yes, string is immutable data type. The content of string once assigned cannot be altered than.
String operations
• Concatenation
• Repetition
• Membership
• Slicing
Concatenation
Repetition
Membership
• Membership operation refers to checking a string or character is part or subpart of an existing string
or not.
• 'in' returns true if the first string or character appears as substring in the second string.
• 'not in' returns true if the first string or character does not appears as substring in the second string.
Slicing
Traversing a String
String Functions