Python Summary

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Variable

_
A-Z A-Z a-z 0-9 _
a-z

Legal Illegal
Myname 2myname
my_name my-name
_myname my name
my_name2 my.name

Comment
a=b=c=10 a,b,c=2,3,6
a = 10 ( int – integer )
a → 10 a→2 b = 2.5 ( float )
b → 10 b→3 c = ‘amal’ ( str – String )
c → 10 c→6
Break

a=1 output
while a<6:
print(a)
if a==3:
break
a=a+1

Exit the loop when a is 3

Continue
output
While True i=0
While loop For Loop
while i<6:
i=i+1
if i==3:
output continue
print(i)
Never Ends

Continue to the next iteration if i is 3


List Output
Age=[12,14,21,8] Age.append(60) [12,14,21,8,60]
(Adding to the last)

Age.insert(2,10)
(adding a new elemnt [12,14,10,21,8,60]
for 2nd position)

Age.remove(10) [12,14,21,8,60]
(removing from the list)

Age.pop(1) index [12,21,8,60]


(delete 1st value)

L1=[1,3,4,3,6] L1.count(3) 2
(How many times)
String
A=[‘Hi how are’] A.Find(‘how’) 3 (index)
‘A’+’B’ → ‘AB’
N=[4,6,1,3,9,8] N.Sort()
‘A’+’ ‘+’B’ → ‘A B’ (ascending oder)
[1,3,4,6,8,9]
N1=[1,2,3]
A=‘Nimal’ N1.extend(N2) [1,2,3,4,5,6]
N2=[4,5,6] (like concatenation)
len(a)
5
[1,2]+[3,4] → [1,2,3,4] L=[1,2,3]
‘AB’*2 3 in L → True
‘ABAB’
Function Local Variable Global Variable

Creating a Function Calling a Function

With parameter

➢ Can only use inside ➢ Can use inside and outside


the function
➢ Need to use ‘global’ key
➢ Normally when we Word to make it global
Create variable inside
a function that variable
Is local
High priority for calling
File Handling Write append Split

XYZ ABC
XYZ

File_object = open(file_name , access_mode) Strip

‘r’ → Read → Opens a file for reading,error if the file


does not exists
‘a’ → append → Opens a file for appending,creates a file
if it does not exists
‘w’ → Write → Opens a file for writing, creates a file
if it does not exists
‘x’ → Create → Creates the specific file , retiiurns an
error if the file exists

You might also like