Python Basics Part 2 Flashcards

1
Q

show lowest value in string

A

A=min(“helloword”)
A
‘d’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how do you sort a list?

A

a.sort()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

insert an element in a list

A

a.insert(0,-3)# index,element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

add an element to a list

A

a.append(30)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

remove a specific element

A

a.remove(34)# remove value 34

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

remove a specific position, or last

A

a.pop(3)#removes value from index 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

program

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

create a dictionary

A

dict1={‘apple’:’Yes’,’Banana’:’No’,’Pizza’:’2’}

dict1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

print value from specific key dictionary

A

print(dict1[‘apple’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

change a value of a dictionary

A

dict1[‘apple’]=’No’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

add a dictionary to another dictionary

A

new=[‘grapes’:’no’]

dict1.update(new)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

remove everything from dict

A

dict1.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

iterate the values in a dictionary and print them

A
for key in dict1,keys():
     print(key)
another:
for key in food.keys():
    print (key)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

another view of keys in dict

A

> > > a_dict = {‘color’: ‘blue’, ‘fruit’: ‘apple’, ‘pet’: ‘dog’}
keys = a_dict.keys()
keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

another method to iterate thru keys in dict

A
# Iterate the keys: Method 2
for key in food:
    print (key)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to iterate over both key and value pairs in dictionary

A

for x in food:

print(x,food[x])

17
Q

iterate over the values in a dictionary

A
# Iterate the values: Method 1
for b in food.values():
    print (b)
18
Q

another way to iterate over values

A

for b in food:

print (food[b])

19
Q

open file

A

fhandle=open(‘mbox.txt’,’r’)

print(fhandle)

20
Q

write code to enter your name and print

A

name = input(“Please enter your name: “) # raw_input converts anything it gets into a string
print(‘Hello,’, name,”!”)

21
Q

have user enter data for hight and width, convert to float and find square footage.

A

width=float(input(‘Please enter the width of the room in feet: ‘)) #float function converts input into a floating point number.
length=float(input(‘Please enter the length of the room in feet: ‘))

print(“The area of this room is “+str(widthlength)+’ square feet’
“The area of this room is”, width
length, ‘square feet’)

22
Q

print sum of 2 values

A

a=int(input(‘please enter number’))
b=int(input(‘please enter another number’))
print (a,”+”,b, “is”, a+b)
you have to put int( in front or it will treat as string
and you’ll get 1010 instead of 20. for 10 and 10

23
Q

print a message if variable larger then 6

A
x=7
if(x>6):
 print('big')
 print('still big')
print('done')
24
Q

print if else comparing a value x=10

A
x=11
if x < 5:
    print('less then 5')
elif x<10:
    print('less then 10')
else:
    print('greater then 10')