Python Basics Part 2 Flashcards
show lowest value in string
A=min(“helloword”)
A
‘d’
how do you sort a list?
a.sort()
insert an element in a list
a.insert(0,-3)# index,element
add an element to a list
a.append(30)
remove a specific element
a.remove(34)# remove value 34
remove a specific position, or last
a.pop(3)#removes value from index 3
program
data_list=[]
num = int(input(“Please enter an integer (0 to quit): “))
while num != 0:
data_list.append(num)
num = int(input(“Please enter an integer (0 to quit): “))
data_list.sort() # or sorted(data_list)
print “The values, sorted into ascendng order, are:”
for num in data_list:
print num
create a dictionary
dict1={‘apple’:’Yes’,’Banana’:’No’,’Pizza’:’2’}
dict1
print value from specific key dictionary
print(dict1[‘apple’])
change a value of a dictionary
dict1[‘apple’]=’No’
add a dictionary to another dictionary
new=[‘grapes’:’no’]
dict1.update(new)
remove everything from dict
dict1.clear()
iterate the values in a dictionary and print them
for key in dict1,keys(): print(key) another: for key in food.keys(): print (key)
another view of keys in dict
> > > a_dict = {‘color’: ‘blue’, ‘fruit’: ‘apple’, ‘pet’: ‘dog’}
keys = a_dict.keys()
keys
another method to iterate thru keys in dict
# Iterate the keys: Method 2 for key in food: print (key)