Created
March 7, 2018 17:52
-
-
Save kennycason/01da696d570024c61fa969ae79cf9dee to your computer and use it in GitHub Desktop.
Python Examples for friend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("hello, world") | |
# list | |
friends = ['john', 'pat', 'gary', 'michael'] | |
print(len(friends)) | |
for f in friends: | |
print("hello " + f) | |
# map / dictionary | |
friends2 = { | |
'jon' : 14, | |
'ken' : 31, | |
'aaron' : 26 | |
} | |
for name, age in friends2.items(): | |
print("hello " + name + ", you are " + str(age) + " years old") | |
# function | |
def add(x, y): | |
return x + y | |
# print(add(1, "frog")) # will fail | |
print(add(1, 2)) | |
print(add("cat", "dog")) | |
# OOP Object Orient Programming | |
class Person: | |
name = "" | |
age = 0 | |
job = "homeless" | |
def __str__(self): | |
return f'name: {self.name}, age: {self.age}, job: {self.job}' # starting with "f" means "format" | |
def salary(self): | |
if self.age < 30: | |
return 10000 | |
else: | |
return 20000 | |
def salary(person): | |
if person.age < 30: | |
return 10000 | |
else: | |
return 20000 | |
aaron = Person() | |
aaron.name = "Aaron" | |
aaron.age = 26 | |
aaron.job = "Vendor Manager" | |
print(aaron) | |
kenny = Person() | |
kenny.name = "Kenny" | |
kenny.age = 31 | |
kenny.job = "Software Engineer" | |
print(kenny) | |
print(f'kennys salary is ${kenny.salary()}') | |
print(f'kennys salary is ${salary(kenny)}') | |
print(f'aarons salary is ${aaron.salary()}') | |
print(f'aarons salary is ${salary(aaron)}') | |
# OOP Constructor/Init | |
class Person2: | |
name = "" | |
age = 0 | |
job = "homeless" | |
def __init__(self, name, age, job): | |
self.name = name | |
self.age = age | |
self.job = job | |
aaron = Person2("Aaron", 26, "Vendor manager") # create via constructor (uses the __init__ function) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment