# Python Notes - Part 02

 What is a for loop? A way by which a task can be repeated through a LIST/TUPLE/DICTIONARY without needing to repeat the task request. Example: list = [‘1’, ‘2’, ‘3’, ‘4’] for number in list:      print(“My favorite number is “ + number + “.”) >>> My favorite number is 1. >>> My favorite number is 2. >>> My favorite number is 3. >>> My favorite number is 4. Print function will run through each item in the list. How does the range() method work? Generates a series of numbers. Example: for value in range(1,5):         Print(value) >>>1>>>2>>>3>>>4 Range() output never contains the end parameter (5 in this case). How does the list() method work? Creates a list. Example: numbers = list(range(1,6)) >>>print(numbers) >>>[1, 2, 3, 4, 5] How can you create intervals (skips) with the range() method? Example: range(2,11,2) # Range begins at 2 and continues till 10 and adds 2 at every number >>>list(range(2,11,2) >>>print(list) >>>[2, 4, 6, 8, 10] How can you find the minimum, maximum and sum total of a list? Use the min(), max() and sum() methods. Example: >>>list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>>min(list) >>>0 >>>max(list) >>>9 >>>sum(list) >>>45 What is slicing in regards to lists? Accessing specifics items from a list. Example: list = [‘charles’, ‘martina’, ‘michael’, ‘florence’, ‘eli’] >>>print(list[0:3]) >>>[‘charles’, ‘martina’, ‘michael’] >>>print(list[1:4]) >>>[‘martina’, ‘michael’, ‘florence’] >>>print(list[:3] # this signifies that the list starts from the very beginning >>>[‘charles’, ‘martina’, ‘michael’] >>>print(list[-3:]) >>>[‘michael’, ‘florence’, ‘eli’] How do you copy a list? Example: list = [‘pizza’, ‘pasta’, ‘sushi’, ‘hamburger’] food = list[:] # new list variable must always be the first placed to create a copy >>>print(food) [‘pizza’, ‘pasta’, ‘sushi’, ‘hamburger’] What is a Tuple? A container for multiple items. A tuple looks just like a list except you use parentheses instead of square bracketsThe items within the Tuple cannot be modified (no removal, exchange or addition) Example:numbers = (200, 50) Only way to modify a tuple is to assign a new series of items to the tuple numbers = (400, 100) What is a Conditional Expression? A way to compare/evaluate an if statement. in and or not inisis not What are Comparison Operators? Operators that compare two values. ==    (Equal to) !=     (Not equal to) <      (Less than) >      (Greater than) <=   (Less than or equal to) >=   (Greater than or equal to) What is an if statement? A conditional that, when it is satisfied, activates some part of a code. Example: >>>a = 20 >>>if a >= 22:             print(“if”)      elif a >= 21:             print(“elif”)      else:             print(“else”) What is a Dictionary? Location where related data are placed. Operate as key-value pairs Curly braces { } mark the beginning and end of a Dictionary. Example: person = {‘first name’: ‘paul’, ‘last name’: ‘smith’, ‘age’: 23} words on the left are the keys words / number on the right are the value >>>print(person[‘first name’]) >>>’paul’ Can a value in a Dictionary be changed/modified? Values can be changed/modified Example: person = {‘first name’: ‘paul’, ‘last name’: ‘smith’, ‘age’: 23} person[‘first name’] = ‘gary’ >>>print(person[‘first name’]) >>>’gary’ Can a Key-Value pair be removed from a Dictionary? Key-Value pairs can be removed from a Dictionary Example: # use del method person = {‘first name’: ‘paul’, ‘last name’: ‘smith’, ‘age’: 23} del person[‘first name’] >>>print(person) >>>{‘last name’: ‘smith’, ‘age’: 23} Can a For Loop be used on a Dictionary? For Loop can be used on a Dictionary. Example: user_0 = {'username': 'efermi', 'first': 'enrico','last': 'fermi',} for key, value in user_0.items(): print("\nKey: " + key) print("Value: " + value) Using the items() method permits the For Loop to go through the keys and values in the Dictionary The key is contained within the (key) variable, the value is contained within the (value) variable Key: username Value: efermi Key: first Value: enrico Key: last Value: fermi How can you loop through all the keys in Dictionary? Using the For Loop and the keys() method Example: favorite_languages = {'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python',} for name in favorite_languages.keys(): print(name.title()) >>>Jen >>>Sarah >>>Phil >>>Edward Note: Looping through the keys is actually the default behavior when looping through a dictionary. Code could be written as such: for name in favorite_languages: How can the Dictionary keys be sorted when processed in a For Loop? Using the sorted() method Example: favorite_languages = {'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python',} for name in sorted(favorite_languages.keys()): print(name.title()) >>>Edward >>>Jen >>>Phil >>>Sarah How can you loop through all the values in a Dictionary? Using the For Loop and the values() method Example: favorite_languages = {'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python',} for language in favorite_languages.values(): print(language.title()) >>>Python >>>C >>>Ruby >>>Python How can you loop through a Dictionary and not include the repeated values? Using the set() method Example: favorite_languages = {'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python',} for language in se(favorite_languages.values()): print(language.title()) >>>Python >>>C >>>Ruby What is nesting? When a list, tuple, dictionary are placed within other lists, tuples or dictionaries. # Dictionaries in a list alien_0 = {'color': 'green', 'points': 5} alien_1 = {'color': 'yellow', 'points': 10} alien_2 = {'color': 'red', 'points': 15} aliens = [alien_0, alien_1, alien_2] # List in a Dictionary pizza = {'crust': 'thick', 'topping : ['mushrooms', 'extra cheese'],} # Dictionary in a Dictionary users = {'aeinstein': {'first': 'albert', 'last': 'einstein', 'location': 'princeton',} Authorfaker1010 ID342594 Card SetPython Notes - Part 02 DescriptionPython Notes - Part 02 Updated2018-10-05T23:31:15Z Show Answers