My Python
My Python
Macro can be defined as an automation wherein events that have been sequenced in a
particular order are executed automatically.
Cross plat forming-the integration of two languages in order to ease out the process to
attain a particular objective, like java+ python= jython
Consoles are separate windows for various purposes that appear upon execution of a
particular command. ‘
Stand- alone applications are the ones which do not require any other application to
function.
Logical operators- the operators which connect two statements…like- and, or etc.
Comments in python
-comments describe a particular code written. These are only displayed in the code
window for the convenience of the script reader. They do not get printed or executed.
-for example-
Or
‘’’hi
This
Is my
First project’’’
Or
“””hi
This is
Beautiful”””
Data types
1. Int(whole numbers)
2. Float (decimals)
3. Longint(long whole number)
4. String
5. Tuple
6. List
Assignment statement
This is in order to assign or store value in a variable.
Variable=expression
Type
Result-
<class 'int'> <class 'str'> <class 'str'> <class 'float'>
Swapping of variables
X=10
y=15
x,y=y,x
print (x,y)
XXXXX
Operations that can be performed on numeric variables-
Multiplication (*)
Addition (+)
Subtraction (-)
Division (/)
Floor division (doesn’t show the decimal part)(//)
Exponentiation (**)
Modulus (shows remainder)(%)
Operations on string
3. Count
X=’namrata’
Print (x.count (‘a’))
Note-
-in order to count the number of as in namrata
-we can do the same with numbers by converting them into
string by putting single quotes.
4. Find
x= ‘namratamunjal’
Print (x.find (‘a’, 2)
.. X=’assassination’
Print (x.find (‘s’, 4)
Note-
-to find the position of a specified character
-this can be done by specifying the number of characters after
which the desired letter has to be counted.
5. Case change
name='ramanujan saarvey'
print(name.upper())
name='ramanujan'
print(name.lower())
name='ramanujan'
print(name.capitalize())-----(convert the first
character to capital)
name='ramanujan'
print(name.title())-------(convert the first
character of the words present to capital)
name='ramanujan'
print(name.swapcase())------(toggle case)
6. Strip method
In order to remove the unwanted character from
the extreme left or extreme right or both.
name='ramanujan'
print(name.rstrip("n"))
name='ramanujan'
print(name.lstrip("r"))
name='1-2-3-4-5-6-7'
name1=(name.split("-"))
8. Join
name='1-2-3-4-5-6-7'
name1=(name.split("-"))
print(':'.join(name1))
9. Replace
name='1-2-3-4-5-6-7'
print(name.replace('7','8'))
10.Justification
name='himani'
print(name.rjust(10,'*'))
name='himani'
print(name.ljust(10,'*'))
name='himani'
print(name.center(10,'*'))
name='himani'
print(name.zfill(10))
Note-
Use comma to define the use of existing variable
name='smith'
name2='cella'
age=20
if name=='smith'and age==20:
print('Salary is $60000')
else :
print('salary is $80000 ')
Example 2-
is_criminalhistrory=False
if is_criminalhistrory:
print('yes')
else:
print('no criminal')
Example 3-
no_criminal_history=True
good_credit_history=False
principal_amount=1000000
time=10
x=8
y=12
if no_criminal_history and good_credit_history:
print('EMI=',(principal_amount*time*x)/100)
else:
print('EMI=',(principal_amount*time*y)/100)
no_criminal_history=True
good_credit_history=False
principal_amount=1000000
time=10
x=8
y=12
if no_criminal_history and
good_credit_history:#THIS MEANS IF BOTH CONDITIONS
ARE TRUE.
print('SI=',(principal_amount*time*x)/100)
else:
print('SI=',(principal_amount*time*y)/100)
x=5
if x>0:
print('x is postive')
elif x==0:
print('x is equal to 0')
else:
print('x is negative')
INPUT FUNCTION
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1['red'])
FUNTIONS
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(len(dict1))
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(max(dict1))
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(min(dict1))
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(list(dict1))
METHODS
COPY METHOD
dict1={'red':'lal','green':'hara','orange':'narang
i'}
d2=dict1.copy()
print(d2)
GET METHOD- IN ORDER TO FIND OUT THE ASSOCIATED
WORD OF A SPECIFIED WORD FROM THE DICTIONARY.
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.get('green'))
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.keys())
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.values())
dict1={'red':'lal','green':'hara','orange':'narang
i'}
print(dict1.items())
dict1={'red':'lal','green':'hara','orange':'narang
i'}
dict2={'red':'LAL'}
dict1.update(dict2)
print(dict1)
dict1={'red':'lal','green':'hara','orange':'narang
i'}
dict1.clear()
print(dict1)
Loops
TUPPLE- storing of multiple values in one variable.
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(type(PRIME_NUM))
LIST
PRIME_NUM=[2,3,5,7,11,13,17,19]
print(type(PRIME_NUM)))
The difference between list and tuple is that the values stored in a variable
of tuple data type can’t be changed.
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(max(PRIME_NUM))
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(min(PRIME_NUM))
PRIME_NUM=(2,3,5,7,11,13,17,19)
print(PRIME_NUM[:])
tupl=('namrata','hardik','pearl')
x,y,z=tupl
print(x)
tupl1=('namrata','hardik','pearl')
tupl2=('riya','siya','kunal')
print(tupl1+tupl2 )
veg=('tomato','spinach','ladyfinger')
fruit=('mango','apple','melon')
p,q,r,s,t,u= veg+fruit
print(t)
ALL THE METHODS ARE SAME FOR LIST AND TUPLE EXCEPT
THAT LIST CAN BE UPDATED.
veg=['tomato','spinach','ladyfinger']
fruit=['mango','apple','melon']
veg[0]='potato'
print(veg)
veg=['tomato','spinach','ladyfinger']
fruit=['mango','apple','melon']
del veg[0]
print(veg)
veg=['tomato','spinach','ladyfinger']
fruit=['mango','apple','melon']
if 'broccoli'in veg:
print('match available')
else: print('match unavailable')
Conversion of list to tuple
x=(1,2,3,4,5,6)
list1=list(x)
print(type(list1))
x=[1,2,3,4,5,6]
tupl=tuple(x)
print(type(tupl))
x=[1,2,13,64,15,6]
print(sorted(x))