Basic Python
#this is first printing
print("hello")
#to know type/int/float/str/bool
type(11)
type(11.09)
type('lucknow')
type(True)
#changing type
float(30)
int(2.8)
int(1)
#expression and variables
#operations and operand
print(38+38+28+24)
print(26/5)
print(30//4)
#variables is used to store values
my_age=29
print(my_age)
#string operations
#string choose value from '0' and can be negative/ last elememt will be[-1]
#slicing [0:4],[0:6] where 4&6 will not be included
#stride-[::2]-it means selection of every second value
my_name='Bhagat Singh'
my_name[0:5]
my_name[::3]
my_name[2:7:2]#it means it will select every second value upto 6th element
len(my_name)
new_name='Sardar ' +my_name
print(new_name)
#int*str will replicate the no.of time str
3*my_name
#escape sequences '\' e.g.\n for new line,\t for tab
print("my name is \n Bhagat Singh")
#strings method
my_name="Bhagat Singh"
my_name.upper()
my_name.lower()
new_name=my_name.replace('Bhagat','Ajit')
print(new_name)
my_name.find('at')
my_name.find('Singh')
#regex for strings tool in python by import re
#search,split,findfall,sub
my_name.split()#split into list
import re
s1='My name is Bhagat singh'
pattern=r'Bhagat'
result= re.search(pattern, s1)
if result:
print("match found")
else:
print("match not found")
pattern=r'\d\d\d\d\d\d\d\d\d'
text='My phone nuber is 1234567890'
match= re.search(pattern, text)
if match:
print('phone no. found:', match.group())
else:
print("no match")
pattern=r'\w'
text1='my name is Bhagat Singh'
matches=re.findall('is', text1)
print('Matches:', matches)
Comments
Post a Comment