Python Rules
Python Rules
2. If you want to input a data then you have to type input before the code keeping all the code in the
bracket.
3. to turn a long sentence or paragraph into the string just enclose the whole paragraph with the triple
apostrophe
4. to print the single character in a string type the index no in a big bracket[].
Example:- st = hey
Print(character) {to get all the characters separately by just one simple coding}
Islower(to ensure the string is not in capital form),,,, isupper,,,, strip(to remove white spaces),,,, rstrip( to
remove any signs after the string),,,,, capitalize( it makes the first letter of the string capital or any other
letter in the middle as small ,,,, find(to find the index of the letter or word in a string),,,, index(same as
find but gives error if wrong word is given),,,istitle(to check whether first letters of each word in a string
are capital),,,, title( to make first letter of each word capital),,,,, replace(to replace a word by
another),,,isalpha,,,,isalnum,,, endswith(to check if the string ends with given character or
not),,,startswith(opposite to endswith),,,,isprintable,,,center(to align the string at the center),,,,count(to
count the number of given characters),,,,, swapcase( to swap the characters form lower to upper and
vice versa) ;;;; reverse(type[::-1] right after the string)
SETS METHODS
#Empty set == e.g. for=set()
#For enabling set use {} brackets
#METHODS == union() to join two sets,,,,, update() to join two sets
without interrupting the other one,,, intersection()to get the
common elements,,, intersection_update() to add the common
elements in a set ,,,, symmetric_difference() to get the non-common
elements in both sets,,, symmetric_difference_update() to add the
non-common elements in a single set,,,, difference() simply refers as
[A-B] ,,,,difference_update()to add the difference elements in a set,,
Isdisjoint() to know whether the sets have any common element or
not,,, issuperset() to know whether a set is a superset of other,,,,
issubset() ,,,,, pop() to get a single element from a set,,, remove() /
discard() to remove any element from a set,,, set.clear()To clear all
the elements of a set and print an empty set,,, set.add() to add any
element in a set,,, del set To delete the entire set,,,
## DICTIONARY IN PYTHON###
---Dictionary is the class type in python which is used to record the name and marks of the
students in a single format. It is enclosed within curly brackets like a set. The key and the
values in dictionary are separated by a ‘:’ sign.
e.g. dic = {45:’anuj’, 60:’pawan’, 70:’Bibek’}
#to get a single value ‘’’print(dic[45])
#to get key and their values separately ‘’’’ print(dic.items())
#to iterate the key and value use for loop
e.g. for key, value in dic.items():
print(key, value)
#to iterate only key or only value again use for loop
#
###DICTIONARY METHODS###
But to delete a single key value pair you can also type ;;;del dic[key]
E.G. try:
For I in range(5):
Print(“f’{int(a),’x’,I,’=’,int(a)*i)
Except:
This will help you to run the entire code if there is any error in the above code.
###FINALLY statement###
##This coding is used to raise error intentionally when you do not want to run the programs further if
‘;;;class CustomError(…..):
code
It is used to change the local variables in any function into the global vriables.
Also, it can be used to change a previous global variable into a new one.
Global x
X=4
Print(x)
Happy()----- output == 4
Print(x) -----output == 4
---= Readline() :- it enables you to read one line of a file after the cursor.
---=Read():- It enbles you to read all the characters of a file after the cursor.
---=tell():- it enables you too know the exact position of the cursor.
---=With open() as x: --- it helps you to open and read or edit a file with demanding a closing statement.
---=read, write, append :- it enables you to read, write and append a statement to the file.
For instance;-
Def double(x):
Return x+2
Map --- it enables users to make a desirable list from the existing list
If L is a list then ;;; sum = list(map(lambda x:x+x,L)) ‘’a new list will be formed accrding to the function
Filter – it enables users to get the desired characters from the existing list
Def get(a): return a>2 ;;;; der = list(filter(get, L));; a new list will be formed according to function get
It should be noted that reduce must be imported from (functools) before use
IS --- enables users to know whether the location of two variables are same or not.
== --- enables users to know whether the value of two variables are equal or not.
###OOPS in python###
OOPS is called as Object Oriented Programming and it is used to create a separate class for a similar
group of objects. For example ;; to create an application form for a university (class) function can be used
to create a class for the form which includes objects like Name, Age, Faculty , etc.
##Constructor##