Python Midterm I
for z in range(-500,500,100): print (z)
-500 -400 -300 -200 -100 0 100 200 300 400
for x in range(5): print (x)
0 1 2 3 4
for x in [0, 5]: print (x)
0 5
for x in range(3): for y in range(4): print (x,y,x+y)
0,0,0 0,1,1 0,2,2 0,3,3 1,0,1 1,1,2 1,2,3 1,3,4 2,0,2 2,1,3 2,2,4 2,3,5
for x in [1,2,3,4,5,]: print(x)
1 2 3 4 5
for p in range(1,10): print (p)
1 2 3 4 5 6 7 8 9
for x in [1,2,3]: for y in [4,5,6]: print (x,y)
1,4 1,5 1,6 2,4 2,5 2,6 3,4 3,5 3,6
for q in range(100,50,-10): print (q)
100 90 80 70 60
print("1234", end"") print("5678")
12345678
print("1234", end=" HELLO") print("5678")
1234HELLO5678
Accumalator Variable
A variable that accumulates value in a loop
Explain what a Boolean expression is and why you would use one while programming. Give three examples.
Boolean expression is an expression that will either resolve to TRUE or FALSE. If statements. While statements.
Describe two different ways to stop a loop from iterating?
Break() or making keepgoing == not yes
Alphabetical comparison
Capitalized letters are always less than lower case
/
Float Division
//
Integer Division (Round Down)
.islower()
Is it lower case? True or false
.isupper()
Is it upper case? True or false
Explain the differences and similarities between the "if" statement and a "while" statement in Python
Similarities: While's and ifs only take boolean expressions. Differences: If statements either resolve to true or false and if false then the program will stop. While statements on the other hand will continually execute a command as long as something is true.
print(string within string)
True or False return
When do you use while() loops
When you dont know how many times it will take to finish the program
When do you use for() loops
When you know how many times you have to go through the program
sep=""
anytime there is a new argument in a print it will add whatevers in the sep
ord()
character converted to an integer value
.isnumeric()
checks to see if value is numeric. Return True or False
"cat" > "dog"
compares ascii values. True or False
char()
converts integer to a character value
.keys
creates list of all the keys in a library
reverse number
devide number by places (10, 100, 1000, etc...) then mod remainder. apply to variable and print.
a = input("number 1: ") #user enters 2 b = input("number 2: ") #user enters 9 c = b * a C = ?
error, cant multiply two strings
word = "foo" while len(word) < 10: print (word) word = word * 2
foo foofoo
libraries
have key and value pairs. The key is first and in order to retrieve the value you must have the key. foo = { "apple":1.99, "pear":2.99, "peach":3.99 }
variable = aron print("hi", variable, "!", sep=":)"
hi :) aron :) !
check list for duplicates
if ___ is in list
Identify three functions that convert data from one data type to another. Provide a practical example of each function and describe when you would use it in an actual program.
int(). float(). str()
4. Name and describe three different data types in Python and when they could be used in a program
integer(chopped decimal, people). float (decimal incuded, money).String (words)
.isalpha
is it alphabetic? True or false
end= " "
keeps on same line and adds space inbetween
<4d
left aligned, 4 wide, integer
len()
length of word
"/n"
line break
%
mod - fit number into other number as many times as possible, remainder is the mod. If number cant fit to begin it is just the original number
for y in range(500,100,100): print ("*", y)
no output
food = "Apple Sauce" print(food[2::2])
peSue
food = "Apple Sauce" print(food[2:7])
ple S
>3s
right aligned, 3 wide, string
>3.2f
right alligned, 3 wide with 2 decimals allowed, float
.sort()
sorts set of values (lists) alphaetically or by value
immutable
strings are immutable because you directly change them with a function. Functions just return new strings
.split()
this function will split a list at the points specified in the parentheticals. for example .split(",") will split the string at every comma.
itterator variable
value in for loop that increases or decreases in value every time it iterates (for ____ in)