Notizen
Python
Inhalt der Notizen (4)
Seite 1
Variables
string = 'this is a string'
int = 34
list = [1,2,3]
dict = {1:1,2:2,3:3}
Seite 2
Function
def squared(num):
return num*num
print( squared(5) )
Seite 3
Classes
class Square :
# here we can define anything shared by all instances of this class
name = 'a squared number'
def __init__(self,num): # self is a convention, __init__ is where we add the attributes
self.num = num #optional
self.squared = num*num
square5 = Square(5).squared #creating an instance but only passing the squared number to square5.
print( square5 ) #so we can print the value here.
square10 = Square(10)#we can also instantiate
print( square10.squared )# then call it's attribute
print( square10.name )# or it's shared attribute
Seite 4
Zip function
itemid = [1,2,3,4,5]
items = ['horses','sheep','cows','chickens','pigs']
item_zip = zip(itemid,items)
list_items = dict(item_zip)
print( list_items ) #will output {1: 'horses', 2: 'sheep', 3: 'cows', 4: 'chickens', 5: 'pigs'}
print( list_items[1] ) # will output horses
Informationen
- Beschreibung
- Python Notiz am Python, erstellt von Jahangir Shah am 01/10/2017.
- Erstellt von
- Jahangir Shah
- Aufrufe
- 0
- Bewertung