Data Keys in Python
pass
does nothing at all
Use a set to find the unique values of the list below: list5 = [1,1,5,6,7,5,4,3,4,5,5,8,8,6,7,1,1,2,3,3,3]
set(list5) {1,2,3,4,5,6,7,8}
*args
Arguements you can add as many arguments as you want ex. def myfunc(*args): print(args) ---- myfunc(40,60,80,100) you don't have to use the word args, you can use whatever as long as it has that * there but do use args so you don't confuse other people reading your code
How do you create a tuple?
Barbra = (1,2,3)
>=
If the value of the left operand is greater or equal to the value of the right operand then the condition becomes true ex. 3 >= 4 is not true
<=
If the value of the left operand is less than or equal to the value of the right operand then the condition becomes true ex. 3<= 4 is true
!=
If the value of two operands is not equal then the condition is true ex. 3 != 4 is true
==
If the values of two operands are equal, then the condition becomes true ex. 3 == 4 is not true
What is unique about a set?
They dont allow for duplicate items!
Control Flow
We often only want certain code to execute when a particular condition has been met. for ex. IF my dog is hungry (some condition), then I will feed the dog (some action). Control flow syntax in python makes use of colons and indentation (whitespace) This indentation system is crucial to python and is what sets it apart from other programming langauges
What is the major difference between tuples and lists?
Tuples are immutable!
Floating Points
Type: Float Numbers with a decimal point. 2.3 4.6 100.0
Booleans
Type: bool logical value indicating True or False
Dictionaries
Type: dict. unordered key: value pairs: {"mykey" : "value" , "name" : "Frankie"}
Integers
Type: int whole numbers such as 3 300 or 200
Build the list [0,0,0] in two separate ways
[0]*3 list2=[0,0,0]
How do you reverse a string?
[::-1] ex. s = 'hello' s[::-1] olleh
Use list comprehension to create a list of the first letters of every word in the string below: st = "Create a list of the first letters of every word in this string"
[word[0] for word in st.split()]
Write a program that prints the integers from 1-100. but for multiples of three print fizz instead of the number. for multiples of three and five print fizzbuzz and for multiples of five print buzz
for num in range(): if num%3 ==0 and num%5=0: print("fizzbuzz") elif num%3 == 0: print("fizz") elif num%5 ==0: print('buzz") else: print(num)
continue
goes to the top of the closest enclosed loop
>
greater than If the value of the left operand is greater than the value of the right operand then the condition becomes true ex. 3 > 4 is not true
**kwargs
key word arguments -builds a dictionary of key value pairs
Give two methods of producing the letter 'o' in 'hello'
s = 'hello' s[4] s[-1]
Return a specific letter?
s = 'hello' s[1] e
while loops
while loops will continue to execute a block of code while some condition remains true -for ex while my pool is not full, keep filling my pool with water -or while my dogs are still hungry, keep feeding my dogs
break
break out of the current closest enclosed loop
Using keys and indexing, grab hello from the following dictionaries: d ={'simple_key':'hello'} d = {'k1':{'k2':'hello'}} d = {'k1':[{'nest key':['this is deep',['hello']]}]}
d['simple_key'] d['k1']['k2'] d['k1'][0]['nest key'][1][0]
Syntax of an if statement
if some_condition: #execute some code elif some_other_condition: #do something different else: #do something else
How do you find a square root?
the number ** 0.5 ex. 100 ** 0.5 = 10
How do you find the square?
the number ** 2 ex. 10 ** 2 = 100
Can you sort a dictionary?
No, bc normal dictionaries are mappings, not a sequence
Lists
Type: list. ordered sequence of objects. [10, "hello", 200.3]
Sets
Type: set. unordered collection of unique objects. {"a", "b"}
Strings
Type: str ordered sequence of characters. "hello" 'Sammy' "2000"
Tuples
Type: tup. ordered immutable sequence of objects. (10, "hello" 200.3)
<
less than If the value of the left operand is less than the value of the right operand then the condition becomes true ex. 3 < 4 is true
Reassign hello in a nested list to say goodbye instead
list3=[1,2,[3,4,'hello']] list3[2][2] = 'goodbye' list3
iterable
many objects in python are iterable, meaning we can iterate over every element in the object -such as every element in a list or string -we can use for loops to execute a block of code for every iteration -you can perform an action for every object
How do you sort a list?
method 1: sorted(list4) method 2: list4.sort()