CH 1
CH 1
CH 1
Chapter – 1
Introduction to Python
Features of Python
1. Easy to use
2. Interpreted language
3. Cross-platform language
Modes in Python:
i) Interactive Mode: It executes one by one command.
ii) Script Mode: It used to execute the multiple instruction (complete
program) at once.
Operators:
An operator is used to perform specific mathematical or logical operation on
values.
i) Arithmetic operators : =,+-,*,//,%
ii) Assignment operators : =,+=,-=
iii) Comparison operators: <,>,==,<=,>=
iv) Logical operators: and, or,NOT
v) Membership operators: in, not in
Input Statement:
Input()-This function first takes the input from the user and converts it into a
string.
name = input('What is your name?\n')
Output using print() function:
print() : function prints the message on the screen or any other standard output
device.
Lists:
A list contains items separated by commas and enclosed within square brackets
([]).
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list ['abcd', 786, 2.23, 'john', 70.2]
print list[0] # Prints first element of the list- abcd
print list[1:3] # Prints elements starting from 2nd till 3rd
- [786, 2.23]
print list[2:] # Prints elements starting from 3rd element -[2.23, 'john', 70.2] print
tinylist * 2 # Prints list two times-[123, 'john', 123, 'john'] print list + tinylist #
Prints concatenated lists-['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Tuples:
A tuple is another sequence data type that is similar to the
list. tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints the complete tuple--('abcd', 786, 2.23, 'john', 70.2)
print tuple[0] # Prints first element of the tuple-- abcd
print tuple[1:3] # Prints elements of the tuple starting from 2nd till 3rd
--(786, 2.23)
print tuple[2:] # Prints elements of the tuple starting from 3rd element--(2.23,
'john', 70.2)
print tinytuple * 2 # Prints the contents of the tuple twice--(123, 'john', 123,
'john')
print tuple + tinytuple # Prints concatenated tuples-- ('abcd', 786, 2.23, 'john',
70.2, 123, 'john')
Dictionary:
Python's dictionaries are kind of hash table type. They consist of key-value pairs
dict = {}
31
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key This is one
print dict[2] # Prints value for 2 key This is two
print tinydict # Prints complete dictionary {'dept': 'sales', 'code': 6734, 'name':
'john'}
print tinydict.keys() # Prints all the keys ['dept', 'code', 'name']
print tinydict.values() # Prints all the values ['sales', 6734, 'john']
Boolean:
Boolean represent one of two values: True or False.
Type conversion:
i) Explicit conversion: int(value) – convert value to an integer. ii)
Implicit conversion: In implicit conversion, data conversion is done
automatically. Var=10.5(float)
Control statements:
There are three types of control statements.
i. Decision Making Statements
ii. Iteration Statements (Loops)
iii. Jump Statements (break, continue, pass)