Python programming
PDF_CODE
HIT137 Software Now
Week 9 class User:
pass
user1 = User()
user1.first_name="Dave"
user1.last_name="Bowman"
print(user1.first_name,user1.last_name)
user2 = User()
user2.first_name="Alex"
user2.last_name="Cole"
print(user2.first_name,user2.last_name)
user1.age=25
user2.favourite_book="ABC Book"
print(user1.first_name,user1.last_name,user1.age)
print(user2.first_name,user2.last_name,user2.favourite_book)
--------------------------------------------------------Refer to PPT
class User:
def __init__(self, full_name, birthday):
self.name=full_name
self.birthday=birthday
user1 = User("Dave Bowman","19710315")
print(user1.name,user1.birthday)
class User:
def __init__(self, full_name, birthday):
self.name=full_name
self.birthday=birthday
name_pieces = full_name.split(" ")
self.first_name=name_pieces[0]
self.last_name=name_pieces[-1]
user1 = User("Dave Bowman","19710315")
print(user1.name)
print(user1.first_name)
print(user1.last_name)
print(user1.birthday)
--------------------------------------------------------Refer to PPT
class User:
"""This class is going to be used for creating user with name
and birthday"""
def __init__(self, full_name, birthday):
self.name=full_name
self.birthday=birthday
name_pieces = full_name.split(" ")
self.first_name=name_pieces[0]
self.last_name=name_pieces[-1]
user1 = User("Dave Bowman","19710315")
print(user1.name)
print(user1.first_name)
print(user1.last_name)
print(user1.birthday)
help(User)
import datetime
class User:
"""This class is going to be used for creating user with name
and birthday"""
def __init__(self, full_name, birthday):
self.name=full_name
self.birthday=birthday
name_pieces = full_name.split(" ")
self.first_name=name_pieces[0]
self.last_name=name_pieces[-1]
def age(self):
"""Return the age of users in years."""
today = datetime.date(2001, 5, 12)
yyyy=int(self.birthday[0:4])
mm=int(self.birthday[4:6])
dd=int(self.birthday[6:8])
dob=datetime.date(yyyy,mm,dd)
age_in_days = (today-dob).days
age_in_years= age_in_days / 365
return int(age_in_years)
user1 = User("Dave Bowman","19710315")
print(user1.name)
print(user1.first_name)
print(user1.last_name)
print(user1.birthday)
print(user1.age())
help(User)
--------------------------------------------------------Refer to PPT
class Employee:
"""This class is going to be used for creating user with name
and birthday"""
empCount=0
def __init__(self, name, salary):
self.name=name
self.salary=salary
Employee.empCount += 1
def dsiplayCount(self):
print("Total Employee %d", Employee.empCount)
def displayEmployee(self):
print("Name :", self.name, ", Salary: ", self.salary)
"""This would create first object of Employee class"""
emp1 = Employee("Zara", 2000)
"""This would create second object of Employee class"""
emp2 = Employee("Manni",5000)
emp1.displayEmployee()
emp2.displayEmployee()
print("Total Employee %d" % Employee.empCount)
class Shark:
def __init__(self,name):
self.name=name
def swim(self):
print(self.name + " is swimming.")
def main():
sam=Shark("Bob")
sam.swim()
if __name__ == "__main__":
main()