Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

PYTHON

Python
• Python is interpreted.
• Can be treated in a Procedural , object oriented or functional way.
• Python follows indentation.
• Python Variables
• IDE
• Spyder, Pycharm etc or use command line.
• Comments
• Use # to comment any line in python
• Variables are created when we assign a value to it.
• Example : x=5
• To print Hello World in Python
• Print (“Hello World”)
Python
• To specify data type of a variable casting can be done
• X=str(3)
• Y=int(3)
• To check the data type of a variable use type
• Print(type(x))
• Variables are case sensitive
• String variables can be defined in single or double quotes both
• Example : x=“string” or x=‘string’
Random Number Generation

Import random
X=random.randrange(1,10)
Print (x)
String
• Immutable objects- cannot be changed
• a=“Hello , World!” #string
• a=“””multi line string
declaration””” H E L L O , W O R L D !
• String are arrays 0 1 2 3 4 5 6 7 8 9 10 11
• Print (a[1]) # H
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

• Looping over array


• For i in a :
print(i)
• Length of string
• Len(a)
String- Slicing
Syntax :
a[start, end , step]

start- starting index


end- ending index and not included H E L L O , W O R L D !
step- steps to jump
0 1 2 3 4 5 6 7 8 9 10 11

Print(a[2:5]) # LL0 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1


Print(a[:5]) # HELL0
Print(a[2:]) # LL0,WORLD!
Print(a[-5:-2]) # ORL
Reverse - Print(a[::-1]) # !DLROW,OLLEH
String-functions
• str.upper() # converts the string to upper case
• str.lower() # converts the string to lower case
• str.title() # converts to proper case
• str.swapcase() # swaps the case

• str.strip([character]) # removes the spaces if no character is passed


• str.zfill(#) # left padding
• str.startswith(character, [start], [end])
• str.rsplit(separator, maxsplit)
• Str.split(separator) # returns list
• str.join(iterable) – returns are string

• str.maketrans(x,y,[z])
• x- what to replace ,y- what should be replace ,z – remove that from string
• Returns a dictionary
• str.translate(Dictionary or maketrans output)
List
• List are mutable ,allows duplicate and changeable
• L=[“apple”,”banana”,”orange”]
• To find the length of the list – len(l)
• To access the item of list –l[0], l[1]…..
• L.insert(position,item)
• L.append(item)
• L.extend(iterable)
• L.remove(item)
• L.pop([index]) # by default last item is removed
• Del l[index]
• Del l
• L.clear()

You might also like