Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
floatingNumber = 10.63587
print(type(floatingNumber)) --> class 'float'
floatingNumber = int(floatingNumber) --> typecasting to int
print(type(floatingNumber)) --> int
print(floatingNumber) --> will print 10 - it entirely dismisses the decimal value
String formatting
print("{} is a great programming language for
beginners".format(programminglanguage))
name = "John"
f'Good morning, {name}. How are you today?'
string.upper()
string.lower()
string.replace("easy" , "powerful")
LIST -
favFruits = ["apple","mango","strawberry"]
operators on the list
favFruits.append("Kiwi");
favFruits.insert(1,"Mango")
favFruits.remove("strawberry")
favFruits.sort() - sorts alphabetically or numberically based on the content
favFruits.reverse()
favFruits.pop() - will remove the last element
TUPLE - with normal bracket
is a collection of immutable(data cannot be changed) objects
historicalWarDate = (1914,1939)
historicalWarDate[1] = 2017 - does not support
del(historicalWarDate)
can create a new tuple from existing tuples
mytuple = (1,2,"hello") = can have multiple datatype
mytuple=("mouse",(1,2,3)) - nested tuple
my_tuple=3,4.6,"dog"
a,b,c = my_tuple
print(a) - 3
print(b) - 4.6
print(c) - dog
Dictionaries
collection of key value pair
#dictionaryname = {key1:value1,key2:value2}
priceOfCamera = {"sony" : 500 , "nikon" : 600 ,"canon" : 700}
priceOfCamera["sony"]
operations on dictionary
priceOfCamera.keys() - will get all the keys of the dictionary
priceOfCamera.values() - will get all the values of the keys
copyofpriceofcameras = priceOfCameras.copy()
del(priceOfCamera["sony"])
priceOfCamera.clear() - will return the empty the dictionary
conditional statements - an
if totalMarks >= 90: - begining of the line
print("congratulations") - should have indentation and the following lines
should have
else ifs
if totalMarks >= 90: - begining of the line
print("congratulations")
elif totalMarks >= 40:
print("u have cleared")
else:
print("u have failed")
nested if
if totalMarks >= 90: - begining of the line
print("congratulations")
if totalMarks = 100:
print("You have cleared with full marks")
number = int(input())
for loop
for fruit in fruits:block of statement that follows the
print (fruit)
numbers = [2, 3, 5]
getsum = [ i+2 for i in numbers ]
print (getsum)
[4,5,7]
numbers = [2, 3, 5]
getnum = [ i+2 for i in numbers if i<5]
print (getnum)
[4,5]
#range()
for number in range(1,10):
print(number)//1 to 9
while number<10
number = 1
for row in range(1,4,1):
for column in range(1,4):
print(number,end =' ')
number = number + 1
print()
Function
def myFirstFunction():
print("My first program")
myFirstFunction()
printMax(3, 4)
4 is maximum
say('Hello')
say('World', 5) - times has default value as 1 else it will print it 5 times
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
func(x)
print('x is still', x)
x is 50
Changed local x to 2
x is still 50
filehandling
set the working directory -
capmdata= open(workingdir+'capm_dem.txt','r')
print ("Content is ", capmdata.read())
newcapmdata = open(workingdir+'capm_dem.txt','r+')
newcapmdata.write ("Sold Value: " '235$')
capmdata.seek(0,0)
capmdata.close()
capmdata.close()
print(newcapmdata.read())
def avgReport(numList):
try:
m= avg(numList)
print ("Average = ", m)
except TypeError as ex:
print ("TypeError:", ex)
except ZeroDivisionError as ex:
print ("ZeroDivisionError:", ex)
finally://always executed
print("Finished avgReport")
try...finally
MemoryError - ram size and lot of dictionary values