ECS 36A Lecture Notes - Lecture 7: Iter, Chromosome, Init

73 views2 pages
Verified Note

Document Summary

Ecs 36a - lecture 7 - classes if word in d: else: d[word] += 1 d[word] = 1. ~this requires checking the dictionary two times per iteration~ try: d[word] += 1 except keyerror: d[word] 1. ~this is more efficient because for more common cases you only have to check the dictionary once ~ Allows you to create objects and associate multiple functions and attributes to each object within a class. Classes help to simplify programs and make them appear more organized. Useful when multiple a program needs multiple functions need access to be able to change and alter the same variables. Will be useful in hw 3 & 4 of this course class human: chromosome = 23 def __init__(self, name, gender): self. name = name self. gender = gender def get_name(self): return self. name print(human. chromosome) # print(human. name) # error alice = human("alice", false) bob = human("bob", true) print(alice. chromosome, alice. name, alice. gender) print(bob. chromosome, bob. name, bob. gender) print(alice. get_name()) print(human. get_name(alice)) prints 23 name is not an attribute.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents