Cs - REVISION TOUR
Cs - REVISION TOUR
In python programming,
data types are inbuilt hence support
“dynamic typing”
declaration of variables is not required.
memory management is automatically
done.
Features of Python
1. Python is an interpreted, interactive,
directly executed language.
(Python Shell)
(Python Editor)
Execution Modes:
1) Interactive Mode :
we can type a Python statement on the >>>
prompt directly. As soon as we press enter, the
interpreter executes the statement and
displays the result(s)
Working in the interactive mode is convenient
for testing a single line code for instant
execution. But in the interactive mode, we
cannot save the statements for future use and
we have to retype the statements to run them
again.
2) Script Mode:
In the script mode, we can write a Python program
in a file, save it and then use the interpreter to
execute the program from the file. Such program
files have a .py extension and they are also known
as scripts.
But for programs having more than a few lines, we
should always save the code in files for future use.
Python scripts can be created using any editor.
Python has a built-in editor called IDLE (Integrated
Development and Learning Environment )which
can be used to create programs. After opening
the IDLE, we can click File>New File to create a
new file, then write our program on that file and
save it with a desired name. By default, the Python
scripts are saved in the Python installation folder.
Python Character Set
A set of valid characters recognized by python.
• Python uses the traditional ASCII character set.
• The latest version recognizes the Unicode character set.
The ASCII character set is a subset of the Unicode
character set.
Letters: a-z,
A-Z
Digits : 0-9
Special symbols : Special symbol available over keyboard
White spaces: blank space, tab, carriage return,
new line, form feed
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Delimiters
1. Keywords
Reserved word of the compiler/interpreter which can’t
be used as identifier.
2. Identifiers
5. Variable names are case sensitive. Num and num are different.
e.g.
print(“I am a student of \n APS \t Hello ”)
4. Operators
Operators can be defined as symbols that are used to
perform operations on operands.
Types of Operators
a. Arithmetic Operators.
b. Relational Operators.
c. Assignment Operators.
d. Logical Operators.
e. Membership Operators
f. Identity Operators
Operators
a. Arithmetic Operators.
Arithmetic Operators are used to perform arithmetic
operations like addition, multiplication, division etc.
print ("hello"+"python")
print(2+3)
print(10-3)
print(22%5)
print(19//5)
print(2**3)
Output:
hellopython
5
7
2
3
8
Operators
b. Relational Operators.
Relational Operators are used to compare the values.
print(5==3) False
print(7>3) True
print(15<7) False
print(3!=2) True
print(7>=8) False
print(3<=4) True
Operators
c. Assignment Operators.
Used to assign values to the variables.
a=10
print(a)
a+=9
print(a)
Output:
b=11 10
b*=3 19
print(b) 33
9.5
c=19
c/=2 16
print(c)
d=2
d**=4
print(d)
Operators
d. Logical Operators.
Logical Operators are used to perform logical
operations on the given two variables or values.
a=30 a=70
b=20 b=20
if(a==30 and b==20):
print("hello")
if(a==30 or b==20):
Output :- print("hello")
hello
Operators
e. Membership Operators
It is used to validate whether a value is found within a
sequence such as strings, lists, or tuples.
E.g. E.g.
a = 22 a = 22
list = [11, 22,33,44] list = [11, 22,33,44]
ans= a in list ans= a not in list
print(ans) print(ans)
Output: True Output: False
Operators
f. Identity Operators
Identity operators in Python compare the memory
locations of two objects.
5. Delimiters
These are the symbols which can be used as
separator of values or to enclose some values.
e.g ( ) {} [ ] , ; :
Token Category
X Variable
y Variable
z Variable
print Keyword
() Delimiter
/ Operator
68 Literal
“x, y, z” Literal
Comment
Comments are used to add a remark or a note in
the source code. Comments are not executed
by interpreter. They are added with the purpose
of making the source code easier for humans to
understand. They are used primarily to document
the meaning and purpose of source code.
In Python, a single line comment starts with #
(hash sign). Everything following the # till the end
of that line is treated as a comment and the
interpreter simply ignores it while executing the
statement.
Multiline comment can be given using
‘’’ Docstring’’’
Example :- ‘’’ this is program for
find maximum of 2 numbers’’’
Write a Python program to find the area of a
rectangle given that its length is 10 units and
breadth is 20 units.
Data types
Floating
Integer Complex Strings Dictionaries
Point
Boolean Lists
Tuples
Data Type
a. int (integer): Integer represents whole
numbers. (positive or negative)
e.g. -6, 0, 23466
b. float (floating point numbers): It
represents numbers with decimal
point.
e.g. -43.2, 6.0
c. Complex numbers: It is made up of pair
of real and imaginary number.
e.g. 2+5j
Data Type
Boolean (bool): It represents one of the
two possible values – True or False.
Backward indexing
It is possible to change one type of value/
variable to another type. It is known as type
conversion or type casting.
Example:
>>> s = set ([1,2,34])
>>> s
output: {1, 2, 34}
Data Type
None: It is a special data type with
single value.
It is used to signify absence of value
evaluating to false in a situation.
1. List
2. Set
3. Dictionary
Immutable type:
Variables whose values can NOT be
changed after they are created and
assigned are called immutable
1. int
2. float
3. decimal
4. complex
5. bool
6. string
7. tuple
Mapping
Mapping is an unordered data type in Python.
Currently, there is only one standard mapping data
type in Python called Dictionary.
(A) Dictionary
Dictionary in Python holds data items in key-value
pairs and Items are enclosed in curly brackets { }.
dictionaries permit faster access to data. Every key
is separated from its value using a colon (:) sign. The
key value pairs of a dictionary can be accessed
using the key. Keys are usually of string type and
their values can be of any data type. In order to
access any value in the dictionary, we have to
specify its key in square brackets [ ].
Example of Dictionary
#create a dictionary
>>> dict1 = {'Fruit':'Apple', 'Climate':'Cold',
'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)':
120}
#getting value by specifying a key
>>> print(dict1['Price(kg)'])
120
Data Type
type() – if you wish to determine type of
the variable.
e.g.
>>> type(10)
<class ‘int’>
>>> type(8.2)
<class ‘float’>
>>> type(“hello”)
<class ‘str’>
>>> type(True)
<class ‘bool’>
Expressions
Expressions are combination of value(s). i.e.
constant, variable and operators.
Expression Value
5+2*4 13
8+12*2-4 28
Implicitconversion,
happens when data type
conversion is done
automatically by Python
and is not instructed by the
programmer.