Professional Documents
Culture Documents
Program
Set of Commands/Instructions.
Programming Language
A way to write set of Instructions.
Ex- Python, C, C++, Java, C#.........
PYTHON LANGUAGE
Python was
designed for
Socket
Python was developed by Programming
Guido Van Rossum in 1990s.
Python was named after the BBC
comedy series ‘Flying Circus’ of
Monty Python. BY:- PUNEET MOHAN
Tokens/Lexical unit
The smallest individual unit in a program is
known as a token.
Python has following tokens:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators
10 not in [10,20,30,40,50]
False
Numbers
String
List
Complex
Tuples
Integer Dictionary
Floating point
BY:- PUNEET MOHAN
Immutable Mutable
▪ Tuple ▪ List
▪ String ▪ Dictionary
▪ Number
Python Screen
Input() Statement
Use to get data/information/string from user.
Ex-
A=input(“Enter your name”)
Or
A=int(input (“Enter a number”))
BY:- PUNEET MOHAN
Variable
Labeled storage location, whose value can be
manipulated during program run.
But python variables are not storage containers,
rather python variables are lie memory references.
Ex- A=10 # Integer value
Name=“Rohan” # String value
A=B=C=10 # Multiple Assignment
A,B,C=10,20,30 #Multiple value to multiple
variable.
Comments in Python
Single line = # Single line comment
Multiple line = ””” Multi line comment”””
BY:- PUNEET MOHAN
Rules to declare Identifier/Variable
•It Cannot start with a digit
•It can contain letters , digits or
underscore
•Punctuation and blanks are not allowed
•Python is case sensitive. The identifier
total is different form Total.
•Keywords cannot be used as a variable.
SEQUENCE
CONDITIONAL/ ITERATION/
SELECTION
STATEMENT LOOP
BY:- PUNEET MOHAN
CONDITIONAL/
SELECTION STATEMENT
•IF… ELSE
•IF…ELIF…ELSE
if expression:
statement(s)
else:
statement(s)
n=int(input("Enter a number"))
if(n>0):
print("Number is Positive")
else:
print("Number is Negative")
n=int(input("Enter a number"))
If(n==0):
print(“Number is Equal to zero”)
elif(n>0):
print("Number is Positive")
else:
print("Number is Negative")
•WHILE LOOP
•FOR LOOP
Initialize
While (Expression):
Statement
Step
N=1
A=int(input(“Enter a number”))
While N<=10 :
print(N*A)
N=N+1
BY:- PUNEET MOHAN
The break Statement
With the break statement we can stop the loop
even if the while condition is true:
Example
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
1 1 12345 * *****
12 22 1234 ** ****
123 333 123 *** ***
1234 4444 12 **** **
12345 5555 1 ***** *
BY:- PUNEET MOHAN
range() Function
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.
number = [2, 3, 5, 7]
for num in number:
print(num)
# Prints 3,4,5
for x in range(3, 6):
print(x)
# Prints 3,5,7
for x in range(3, 8, 2):
print(x)
BY:- PUNEET MOHAN
#Factorial of a Number
n=int(input("Enter a number"))
f=1
for i in range(1,n+1):
f=f*i
print("Factorial= ",f)
Example:
Reverse String
print(s[-1:-6:-1])
print(s[-1: :-1])
BY:- PUNEET MOHAN
String by using ‘in’ operator
s="Hello"
for d in s:
print(d)
s="Hello" s="Bye"
for d in range (0,5) : for d in range(-1,-4,-1):
print(s[d]) print(s[d])
In
‘e’ in ‘Hello’ = True
s=[“English“,45,’maths’,123,’ok’]
for d in range(-1,-4,-1):
print(s[d])
In
10 in [1,10,30,40] = True
To find length
len(L)
To find max Built-in Functions
max(L)
To find min
min(L)
L=["hello","bye","chirag"]
#Creating empty list M[]
M=[]
for i in L:
M.append(i[1:])
#New list
print(M)
BY:- PUNEET MOHAN
TUPLE
Sequence of immutable objects comma
separated values enclosed in parenthesis.
Each item can be individually accessed
using its index.
Valid T=3, / T=(3,)
T=(‘H’,1,”hell”,65.8,’@’)
0 1 2 3 4
Ex-
H 1 “hell” 65.8 “@”
-5 -4 -3 -2 -1
BY:- PUNEET MOHAN
Tuple Accessing
T=(“English“,45,’maths’,123,’ok’)
print(T[0])
print(T[3])
print(T) Seq[start:stop:step]
print(T[-1]) T[0:5:1]
print(T[2:-2])
print(T[1: ])
BY:- PUNEET MOHAN
Reverse Tuple
print(T[-1:-6:-1])
print(T[-1: :-1])
T=(“English“,45,’maths’,123,’ok’)
for d in range(-1,-4,-1):
print(T[d])
In
10 in (1,10,30,40) = True
for n in range(5):
x=int(input("Enter number"))
T=(x, x**2, x**3, x**4)
print(x ,"raised to power 1,2,3,4 :=",T)
T=(23,34,45,56,68,89)
maxval=max(T)
Len=len(T)
secmax=0
for a in range(Len):
if secmax < T[a] < maxval: #68<89<89
secmax=T[a] #68
print("Second maximum value ",secmax)
D={Key: value}
Immutable Mutable
Note- Dictionaries are also called Associative arrays,
mappings or hashes. BY:- PUNEET MOHAN
Characteristics of a Dictionary
➢Unordered set
➢Not a Sequence
➢Indexed by Keys, Not numbers
➢Keys must be unique
➢Mutable
➢Internally stored as Mappings.
Key
EnO Teaches Name Salary
Value
BY:- PUNEET MOHAN
Dictionary Accessing
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }
Print(D[“Eno”])
Print(D)
Print(“Employee No =”, D[“Eno”])
Print(“Emp Name =”, D[“Name”])
To find length
len(D)
To get keys
D.keys()
To get values
D.values()
To delete all elements
D.clear()
BY:- PUNEET MOHAN
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }
D2={ “Eno”: 2, “Teachs”:”Science”,
”Name”:”Suneet”, “Salary”:200000 }
Legal Warning: -
Unauthorized reproduction, retaining
and use of this material, process and
pattern shall be liable to legal action.
BY:- PUNEET MOHAN