Crash Course On Python
Crash Course On Python
Python
By Google
Short Notes By Nithish
Week 1:
<-> Variables:
EX: length=4
width=5
area=length*width
<->Expressions , Numbers and Type conversions:
->Implicit Conversion:
The interpreter automatically converts one data type into another
EX: print(1+1.5)2.5 [ // Interpreter converts int 1 to float ]
print(1+”1.5”)Error
->Explicit Conversion :
The type conversion to be done by us by our need
EX: print(1+int(“1.5”)) 2.5[//We type casted string into int]
.
<->String Methods:
string.lower() / string.upper() Returns a copy of the string with all
lower / upper case characters
string.lstrip() / string.rstrip() / string.strip() Returns a copy of
the string without left / right / left or right whitespace
string.count(substring) Returns the number of times substring is pres
ent in the string
string.isnumeric() Returns True if there are only numeric characters
in the string. If not, returns False.
string.isalpha() Returns True if there are only alphabetic characters
in the string. If not, returns False.
string.split() / string.split(delimiter) Returns a list of substrings
that were separated by whitespace / delimiter
string.replace(old, new) Returns a new string where all occurrences o
f old have been replaced by new.
delimiter.join(list of strings) Returns a new string with all the str
ings joined by the delimiter
.
<-> Strings :
EXAMPLES:
name="Nithish Kumar“
(1).print(name[3:6]) “his”
(2).print(name*3) “Nithish KumarNithish KumarNithish
Kumar”
(3).print(len(name)) 13
(4).print(name[-1] ,name[-2],name[-5]) “r” “a” “k”
(5).print(name[3:]) “hish Kumar”
(6).print(name.index("h")) 3
(7).print( "Nit" in name ) True
(8).print(name.upper()) “NITHISH KUMAR”
(9).print(name.lower()) “nithish kumar”
(10).print("
. chinni ".strip()) “chinni”
(11).print(" chinni ".lstrip()) “chinni “
(12).print(" chinni ".rstrip()) “ chinni”
(13).print(name.count("i")) 2
(14).print(name.endswith("mar")) True
(15).print("123984".isnumeric()) True
(16).print(" Ni ".join(["This","a","b","c","d"]))
”This Ni a Ni b Ni c Ni d”
(17).print("This is a example of split method".split())
[“This” , ”is” , “a” , “example” , “of” , “split” , “method”]
number=len(name)*3
(18).print("Hello {}, your lucky num is {}".format(name,number))
”Hello Nithish Kumar, your lucky num is 39”
(19).print("Hello {name}, your lucky num is {num}".format(name=name,num=len(name
)*3))
”Hello Nithish Kumar, your lucky num is 39”
Formatting Expressions:
{:d} integer value '{:d}'.format(10.5) → '10'
{:.2f} floating point with that many decimals '{:.2f}'.f
ormat(0.5) → '0.50'
{:.2s} string with that many characters '{:.2s}'.f
ormat('Python') → 'Py'
{:<6s} string aligned to the left that many spaces '{
:<6s}'.format('Py') → 'Py '
{:>6s} string aligned to the right that many spaces '{
:>6s}'.format('Py') → ' Py'
{:^6s} string centered in that many spaces '{:^6s}'.f
ormat('Py') → ' Py '
Lists:
EXAMPLES:
friends=['Nithish','Kumar','Sahash','Chinni','Santhu']
(1).for friend in friends:
print("Hi "+friend)
(2).print(friends[1]) “Kumar”
(3).print(friends[2:]) “[“Sahash” , ”Chinni” , ”Santhu”]
(4).print(len(friends)) 5
(5).print(friends[-3]) “Sahash”
(6).print(friends.index("Kumar")) 1
(7).print('Sahash' in friends) True
(8).print(friends[1].upper()) “KUMAR”
(9).print(friends[2].lower()) “SAHASH”
. (10).friends.append("Ash")
print(friends) [“Nithish” , “Kumar” , “Sahash” ,
“Chinni” , “Santhu” , “Chinni”]
(11).friends.insert(1,"Pikachu")
print(friends)
(12).friends.remove("Pikachu")
print(friends)
(13).friends.pop(3)
print(friends)
winners=[ “Nithish” , ”Prem” , ”saiki”]
for index , person in enumerate(winners):
print(“ {} – {} “.format(index+1,person))
Output:
1 – “Nithish”
2 – “Prem”
3 – “saiki”
EX: multiples = [x*7 for x in range(1,11)] {//List Comprehensions}
print(multiples) [7,14,21,28,35,42,49,56,63,70]
<->Tuple:
<->Methods:
dict.get(key, default) - Returns the element corresponding
to key, or default if it's not present
dict.keys() - Returns a sequence containing the keys in th
e dictionary
dict.values() - Returns a sequence containing the values i
n the dictionary
dict.update(other_dictionary) - Updates the dictionary wit
h the items coming from the other dictionary. Existing ent
ries will be replaced; new entries will be added.
dict.clear() - Removes all the items of the dictionary
<---->print(dir(“str”)) gives the methods of string
<---->print(help(“str”)) gives the information about string
methods
.
EX:
wdct={"c":10,"pyt":23,"html":5,"css":7,"js":11}
(1).print(type(wdct))
(2).print(wdct.keys())
(3).print(wdct.values())
(4).print("css" in wdct)
(5).print(wdct['html'])
(6).wdct["txt"]=9
print(wdct)
(7).del wdct['html’]
print(wdct)
(8).for exten in wdct:
print(exten)
.
(10).def letter_count(text):
result={}
for letter in text:
if letter not in result:
result[letter]=0
result[letter]+=1
return result
print(letter_count("This is a string"))
Week 5:
<->OOPS:
EXAMPLES:
(1). class apple:
color=""
flavour=""
goldenapple=apple()
goldenapple.color="red"
goldenapple.flavour="Sweet"
print(goldenapple.color) “red”
print(goldenapple.flavour) “Sweet”
(2).class piglet:
. name=""
def speak(self):
print("Oink ! I am {} Oink!".format(self.name))
hamlet=piglet()
hamlet.name="Hamlet"
hamlet.speak() “Oink ! I am Hamlet Oink!”
(3).class goldApple:
“””This is a docstring which tells us about the what something does”””
def __init__(self,color,flavour):
self.color=color
self.flavour=flavour
def __str__(self):
EXAMPLES:
(1). import random [//This imports the random module]
print(random.randint(1,10)) prints a random num
btw 1-10
(2). import datetime
print(datetime.datetime.now()) prints the date and time
EX: num=[7,5,9,1,4]
print(sorted(num)) [1,4,5,7,9]
print(num) [7,5,9,1,4]
print(num.sort()) [1,4,5,7,9]
print(num) [1,4,5,7,9]