Python is dynamic Programming language?
A dynamic language (Lisp, Perl, Python, Ruby) is designed to optimize programmer
efficiency, so you can implement functionality with less code.
A static language (C, C++, etc) is designed to optimize hardware efficiency, so
that the code you write executes as quickly as possible.
#Input() Function:-Always takes Input in String
Ex:-number=input("Enter the number")
Here,number is a string to convert it into int or float we have,
int(number),float(number):-these functions convert string to int or float
str(number):-this will convert number to string
==>How to Declare Multiple Variables in a Single Line?
Ex:- name,age="Himanshu",19
x=y=z=a=12
==>Taking two or more inputs in a single Line?
name,age=input("Enter your name and age").split() <==>You have to provide space
between name and age while input
name,age=input("Enter your name and age").split(",") <==>You have to provide ,
between name and age while input
String Formatting
Ex:-print("Hello {} Your Age is {}".format(name,age+2)) ==>Python 3({} are called
placeholders)
print(f"Hello {name} Your Age is {age+2}")==>Python 3.6
String Indexing
From Starting:-O to n-1(n is str length)
From Last:- -1 to -n
Ex:-name="Himanshu"
print(name[-8]) Output:-H
String Slicing/Substring
Syntax:- print(string[startindex:endindex])==>It will print String starting from
startindex till (endindex-1)
Ex:-lang="python"
print(lang[0:2])==>py
print(lang[-3:5])==>hon
print(lang[1:])==>ython
print(lang[:])==>python
print(lang[:2])==>py
Step Argument
Syntax:- print(string[startindex:endindex:Step argument])==>It will print String
starting from startindex till (endindex-1) taking the gap of stepargument-1
Ex:- print("Himanshu"[0:6:1])==>Himansh
print("Himanshu"[0:6:2])==>Hmnh
print("Himanshu"[:6:3])==>Hah
print("Himanshu"[6::-1)==>hsnamiH
print("Himanshu"[::-1]) or print("Himanshu"[-1::-1])==>uhsnamiH(Or trick to
reverse a string)ion
String Methods
1.len() function
Syntax:-len(st)==>It will return length of st
print(len(st))==>It will print length of st
Ex:- print(len("Himanshu Pathak"))==>15
2.lower() method
Ex:-
t="hImaNsHu pAtHak"
print(t.lower())==>himanshu pathak
3.upper() method
Ex:-
t="hImaNsHu pAtHak"
print(t.upper())==>HIMANSHU PATHAK
4.title() method
Ex:-
t="hImaNsHu pAtHak"
print(t.title())==>Himanshu Pathak
5.count() method
Ex:-
t="hImaNsHu pAtHak"
print(t.count("H"))==>2
6.strip() method
Ex:-
t=" Himanshu "
print(t.strip())==>"Himanshu"
print(t.lstrip())==>"Himanshu "
print(t.rstrip())==>" Himanshu"
7.replace() method
Ex:-
t="She is beautiful and she is a good dancer"
print(t.replace(" ","_"))==>"She_is_beautiful_and_she_is_a_good_dancer"
print(t.replace(" ","_",2))==>She_is_beautiful and she is a good dancer
print(t.replace("is","was",1))==>"She was beautiful and she is a good dancer"
print(t.replace("is","was",2))==>"She was beautiful and she was a good dancer"
print(t.replace("is","was"))==>"She was beautiful and she is a good dancer"
8.find() method
Ex:-
t="She is beautiful and she is a good dancer"
print(t.find("is"))==>4
print(t.fnd("is",5))==>25(It will start finding from fifth index)
9.center() method
Ex:-
t="Himanshu"
print(t.center(10,"*"))==>"*Himanshu*"(Here 10 is the total length after adding
*(string) to both sides)
print(t.center(12,"*"))==>"**Himanshu**"
print(t.center(9,"*"))==>"*Himanshu"
print(t.center(11,"*"))==>"**Himanshu*"
***Strings are IMMUTABLE*****
Assignment Operator
name+="it"
age+=1
If statement:-
Syntax:- if condition: \\Colon(:) is necessary here
Ex:-age=int(input("Enter your age:-"))
if age>=14:
\\space is necessary after if
print("You are above 14") \\Here the statements which are having tab space
at starting are inside if statement
print("WWW") \\this is outside if statement because it does not
have tab space at starting
pass statement:-The pass statement in Python is used when a statement is required
syntactically but you do not want any command or code to execute. The pass
statement is a null operation; nothing happens when it executes.
Ex:-
x=18
if x==18:
pass \\It does not show any output
if else statement:-
Ex:-
age=int(input("Enter your age:-"))
if age>=14:
print("You are above 14")
else:
print("You are less than 14")
Guessing game by generating a random number and use of nested if else:-
import random
w=random.randint(0,10)
g=int(input("Guess:-"))
if w==g:
print("You Win!!")
print(w)
else:
if w>g:
print("too low")
print(w)
else:
print("too high")
print(w)
And and or operators:-
Syntax:- Condition1 and Condition2 ==>True if both true
Condition1 or Condition2 ==>True atleast one is true
Ex:-1.
age=19
name="himanshu"
if name=="Himanshu" or age==19:==>TRUE \\If and is used then it is FALSE
print("TRUe")
else:
print("False")
2.
name,age=input("Enter name and then age:-").split()
age=int(age)
if (name[0]=="a" or name[0]=="A") and age>=10:
print("You can Watch COCO")
else:
print("You cant")
if-elif-else statement:-
Ex:-
age=int(input("Enter your age:-"))
if 0<age<=3:
price="free"
elif 3<age<=10:
price=150
elif 10<age<=60:
price=250
else:
price=200
print("Price is {}".format(price))
in Keyword:-
The in keyword has two purposes:
The in keyword is used to check if a value is present in a sequence (list, range,
string etc.).
The in keyword is also used to iterate through a sequence in a for loop:
Ex:-
1.For strings
name="Himanshu"
if "H" in name:
print("Present")
else:
print("Not present")
2.For lists
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("yes")
3.For for loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
To check whether string is empty or not:-
name=input("Enter your name:-")
if name:
print("Your name is {}".format(name))
else:
print("You didnt type name")
while loop:-
Ex:-
1.To print hello world 10 times
i=0
while i<10:
print("hello world")
i+=1
2.Sum of Numbers
i=1
sum=0
while i<=10:
sum+=i
i+=1
print(sum)
3.Sum of digits of a Number
n=input("Enter Number:-")
n1=len(n)
i=0
sum=0
while i<n1:
sum+=int(n[i])
i+=1
print(sum)
4.To count distinct characters in a number
name=input("Enter your Name:-")
i=0
temp=""
while i<len(name):
if name[i] not in temp:
temp+=name[i]
print("{} : {}".format(name[i],name.count(name[i])))
i+=1
Infinite Loop
while True:
print("hello")
Boolean Values:-True and False(T and F are capital)
For loop:-
Ex:-print hello world 10 times(Here range(10) means i ki value 0 se 9 tak jaayegi)
for i in range(10):
print("Hello World")
Ex:-2
for i in range(1,11): \\Now it will run for i=1(starting) to i=11-1=10(ending)
print("hello world {}".format(i))
Ex:-3 to calculate sum
sum=0
for i in range(11):
sum+=i
print(sum)
Ex:-4 Sum of digits of a number
n=input("Enter a Number:-")
sum=0
for i in range(len(n)):
sum+=int(n[i])
print(sum)
==>break and continue statement in for loop
for i in range(10):
if i==5:
break
print(i)
for i in range(10):
if i==5:
break
print(i)
==>Step argument in for loop
for i in range(1,11,2): \\It will print 1,3,5,7,9
print(i)
for i in range(11,0,-1): \\It will print 10,9,8....1
print(i)
==>for loop with Strings
Ex:-1
for i in input("Enter name:-"):
print(i)
Ex:-2 Sum of digits
sum=0
for i in input("Enter a no.:-"):
sum+=int(i)
print(sum)