Lecture 3
Lecture 3
keys()
• d.values()
• d.items()
• del(d)
• d.clear()
• del d[‘key’]
Tuples
Tuples are very similar to lists. However they have one key difference -
immutability.
Once an element is inside a tuple, it can not be reassigned.
Tuples use parenthesis: (1,2,3)
A Python tuple consists of a number of values separated by commas.
Unlike lists, however, tuples are enclosed within parentheses.
• cannot be updated. Tuples can be thought of as read-only lists
• len(t)
• type(t)
• # Use .index to enter a value and return the index
• t.index('one')
• # Use .count to count the number of times a value appears
• t.count('one')
• t[0]= 'change‘ ((Immutability))
• del t
• max(t)
• min(t)
• t.append('nope') ((AttributeError ))
Sets
Sets are unordered collections of unique elements.
Meaning there can only be one representative of the same object.
• x = set()
• # We add to sets with the add() method
• x.add(1)
• x.add(1)
• # Create a list with repeats
• list1 = [1,1,2,2,3,4,5,6,1,1]
• # Cast as set to get unique values
• set(list1)
Python Data Type Conversion
• Range
• The range function allows you to quickly generate a list of integers,
There are 3 parameters you can pass, a start, a stop, and a step size.
Let's see some examples:
• list(range(0,11))
Zip
• We can use the zip() function to quickly create a list of tuples by
"zipping" up together two lists.
• mylist1 = [1,2,3,4,5]
• mylist2 = ['a','b','c','d','e']
• list(zip(mylist1,mylist2))
• enumerate
index_count = 0
for letter in 'abcde':
print("At index {} the letter is {}".format(index_count,letter))
index_count += 1
---------------------------------------------------------------------------------
for i,letter in enumerate('abcde'):
print("At index {} the letter is {}".format(i,letter))
• in operator
• we can also use it to quickly check if an object is in a list
• 'x' in ['x','y','z']
• not in
• We can combine in with a not operator, to check if some object or
variable is not present in a list.
• 'x' not in ['x','y','z']
• min and max
• Quickly check the minimum or maximum of a list with these
functions.
• mylist = [10,20,30,40,100]
• min(mylist)
• max(mylist)
• random
• Python comes with a built in random library. There are a lot of
functions included in this random library, so we will only show you
two useful functions for now.
from random import shuffle
shuffle(mylist)
from random import randint
randint(0,100)
List Comprehensions
mystring="Hello Word!"
List1=[]
for x in mystring:
List1.append(x)
print(List1)
-----------------------------------------------------------
mystring="Hello Word!"
mylist=[x for x in mystring ]
print (mylist)
-----------------------------------------------------------
mylist=[x**2 for x in range(1,20) ]
print (mylist)
If condition exercise
• SI, Metric Units:
• BMI = weight(kg)/height2 (m)
• BMI Categories:
Underweight = <18.5
Normal weight = 18.5–24.9
Overweight = 25–29.9
Obesity = BMI of 30 or greater
Test
• Use range() to print all the even numbers from 0 to 10.
• Write a program that works out whether if a given year is a leap year.
A normal year has 365 days, leap years have 366, with an extra day in
February. The reason why we have leap years is really fascinating, This
is how you work out whether if a particular year is a leap year.
• on every year that is evenly divisible by 4
• **except** every year that is evenly divisible by 100
• **unless** the year is also evenly divisible by 400
Leap Year