Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Type casting :- It is the process of conversion of one data type to another data type.

For example, if the


code is written in string then (int) is used to convert into integer and for vice versa (str) is used.

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

Example:- apple =’’’ he is the boy I am looking for.

I am glad that I found him.’’’ And now this becomes a string.

4. to print the single character in a string type the index no in a big bracket[].

Example:- st = hey

Type- Print(st[0]) { to print only ‘h’.}

5. use front loop to get all the characters in a string separately.

Example :- type- for character in st:

Print(character) {to get all the characters separately by just one simple coding}

###string methods in python######

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)

### List methods ###


L = [ 3,5,67,0]
1. Append:- to put any item at the end of the list ;;; L.append(2)
2. Sort :- to arrange the items of list in ascending order;; L.sort() and
L.sort(reverse=True) to arrange in descending order
3. Reverse:- to get the list items in reverse form
4. Copy :- to copy any list inorder to make changes ;;;;m =
L.copy() ;;;m[2]=67 which makes another list ‘m’ with same items
as L but index 2 will replaced by ‘67’
5. Insert:- to insert any item in the list at the given
index;;; ;;;;;;;;;;;;L.insert(2-index,99-object to insert)
6. To add two lists just simply use + sign between them and print
them
##Funtions in Python
1.Variable input arguments – enables you to input values of your
wish;;;;;;;def average(a,b):
Mean=(a+b)/2 print(mean)
Average(4,5)
2.Default arguments- enables you to put the input directly while
creating a function;;;;;;;def average(a=2,b=4) ;;;;computer will take
both inputs as default unless, you provide another one.
3.Keyword arguments – enables to input the values in any order ;;;;;
For the function average input can also be given as
Average(b=4,a=2)by using keyword arguments
4.Arbitary arguments -enables you to iterate all the numbers ;;;;
;;;;def average(*numbers):
For k in numbers;
Sum = sum+k
Print(sum)
5. keyword arbitary arguments – enables you to build up a dictionary
to create a function ..;;;;def average(**name)
Name=[ename =’anuj’,fname=’bunny’,gname=’harry’]
Print(‘Hello’, ename, fname, gname)
# Fstrings in python = This basically helps you to insert any string or
number in a separate string in the middle of the code.
;;;;;txt = f’ my name is {name}.’
name= Anuj
Print(txt)
##Doc strings = It is basically an statement written just below a
function to describe what a function is all about.
##Recursion = It is basically repeating or recalling the function.
;;;;def sum(a,b,c):
Add = a + sum(b,c)
Print(add)

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###

#Update = to add to dictionary items in one

#clear = to clear the items of dictionary and print empty one;;e.g.dec.clear()

#pop(key) = to remove a item from a dictionary’’e.g. dic.pop(12)

#popitem() = to remove the last key value pair from a dictionary

#del = to delete the whole dictionary’’’e.g. delete dic

But to delete a single key value pair you can also type ;;;del dic[key]

###SHORT HAND IF ELSE####

This is used when you have to apply a short condition of if else

For example:- print(‘’…..”) if a<b else print(“……..’’)


###TRY AND EXCEPT METHOD###

#It is generally used to avoid error while doing a program

E.G. try:

A = “enter the number:”

For I in range(5):

Print(“f’{int(a),’x’,I,’=’,int(a)*i)

Except:

Print(‘some error occurred’)

This will help you to run the entire code if there is any error in the above code.

###FINALLY statement###

#This statement is used when we have to execute a code anyhow..

For example in function…

####RAISING CUSTOM ERRORS###

##This coding is used to raise error intentionally when you do not want to run the programs further if

the given input is wrong.

##There are many errors in python and can be seen on chrome.

##Error can be made by ourselves by using

‘;;;class CustomError(…..):

code

### some notes ###


1. T0 convert a string into list simply use (list) comment;;;;b = list(str)
2. To convert list into string use join pattern;;; b = (‘’ “. Join(list)) ;
print(b)
3. pop(index) is used to remove a character in a list.
###ENUMERATE FUNCTION###
#- It is used to locate the index number in a loop without assigning any value to index.
Eg:- for index, characters in enumerate(list):
Print(charaters)
If(index==2):
Print(‘hey’)
###import function###
This function helps you to import all the data and codes from the any python library like math,
pandas, etc.
Import math
M = math.sqrt(9)
Print(M) output = 3
### From### It is used to import the desired functions from a library.
From math import sqrt,pi
M = sqrt(9)*pi
Print(M) output = 9.42…
#### as ### it is used to import a certain functions or codes from the library as you want.
;;; from math import sqrt as s
M = s(9)
Print(M) ;; output = 3
###dir## It is used to get all the functions or codes that are provided by the library in python.
Import math
Print(dir(math));;;;output :- all the functions included in math library.
###_ _name_ _==”_ _main”
:-when a code is imported from somewhere else(or from another file), the result of imported
file may be also executed while running the main program. So, to avoid this mistake _ _name_ _
is used which enables you to run the desired file you want to work on.
e.g. :- If _ _name_ _==’_ _main_ _’
### IMPORT IS ALSO USED TO MAKE FILES AND FOLDERS AND ARRANGE THEM IN AN ORDER##
;;;-- Import os :- is used to import the operating system module in your pc and helps you to make
folders/directory of arrange the files in a folder

#os.getcwd – to know the current working directory

#os.listdir – to know all the directories present in the current directory

#os.chdir – to change directory into desired directory

#os.mkdir – to make a particular directory

#os.makedirs – to make main folder and also files inside them

#os.removedirs – to delete the folder and files included in them

### LOCAL AND GLOBAL VARIABLES###

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.

Ex.;;;; def happy():

Global x

X=4

Print(x)

Happy()----- output == 4

Print(x) -----output == 4

## File Handling ###

---= 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.

---=seek():- it helps you to bring the cursor at the desired position.

---=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.

### Lambda function###

It assists you to create a shortcut function.

For instance;-
Def double(x):

Return x+2

Which can also be written as

Double= lambda x: x+2

##MAP ## FILTER## REDUCE##

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

Reduce – it enables users to reduce a list to one character.

It should be noted that reduce must be imported from (functools) before use

### ‘IS’ vs ‘==’ ###

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.

Class form(name, age,faculty):

Print(f’{name} is {age} years old and he chose {faculty} to study.’)

##Constructor##

It is used to create a separate


###INHEIRTENCE ####
 It is used to make another class which includes all the propertied of the previous class ,
in addition to its own properties. Eg;; if employee is an existing class , another class
‘programmer’ can be made which has all the properties of class employee.
;;;;class programmer(employee):

You might also like