1/42
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
print()
The ______ statement is a command built in to Python. When you run the
.py file, the Python interpreter knows to display whatever is contained
between the ().
b = 6
print(b)
Print the value of a variable "b" that equals 6.
integer
A variable type that holds whole numbers
float
A variable type containing a decimal point
string
The value of this variable type doesn't necessary need to be a letter. We can also
have ______s that are numbers.
print(c)
Use this statement to check the value of a variable "c"
boolean
A Python variable which can only have two
values, True or False. These True of False values reduce down to a 1 or
0, on or off.
myBool = True
print(myBool)
Create a boolean variable that is set to true (case sensitive)
list
''A variable type which can store multiple items together as one variable, and each of those items can actually have
different types.
For example, the first item
could be integer, second item a float, and third item a string.
myList = [4, 6.5, 'a']
print(myList)
'''
myList has three items in and to access each individual item, we use
the index of the item. Indexing starts at 0, so the first item is
myList[0], the second is myList[1], and the third is myList[2]
'''
print(myList[0])
print(myList[1])
print(myList[2])
myList = [4, 6.5, 'a']
print(myList)
Print the list "myList," with values of 4, 6.5, and 'a' respectively.
print(myList[0])
Index the first item in the list "myList"
cast
There are times when you will want to take a variable that is one type and make it behave like it's a different type. We call this ______ing. For
example, if you have a string and you want to be a number, you would ______ it as a float.
x = float(x)
print(x)
Cast the string "x" as a float (take the value at the x location, convert it to a float,
and return it back to the x location.)
x = int(x)
print(x)
Convert the existing value of x to an integer x. If x has a decimal, it will be truncated, and you
will be left with the whole number portion only.
division
Name the operation.
a = 5
b = 6
c = a / b
(Note: c is a floating point number)
multiplication
Name that operation.
z = a*b
print(z)
exponentation
z = a**2
print(z)
addition, multiplication, division
z2 = a + (b * z) / a
print(z2)
radius = 10
area = 3.14 radius*2
circ = 2 3.14 radius
print("the area is:",area)
print("the circumference is:",circ)
Given a circle with a radius of 10 units. Calculate the area and circumference ("circ") of the circle.
The formula for area is a = pi*r^2
The formula for circumference is 2pir
seconds = 200
mins = int(seconds / 60)
s = seconds % 60
print("200 seconds is ",mins, " mins and ", s, "seconds")
Print 200 seconds as a mixed expression of minutes and seconds.
modulus (%)
To convert decimals to mixed expressions, use the ______. (i.e., to convert 200 seconds into minutes and seconds, use the ______ of dividing by 60.)
if mpg < 15:
print("uber polluter")
elif mpg > 30:
print("nice job")
else:
print("not bad")
#if mpg < 15, print uber polluter
#if mpg > 30, print nice job
#if mpg between 15 and 30, print not bad
<
less than
>
greater than
<=
less than or equal to
>=
greater than or equal to
==
equal to
!=
not equal
if a == 3:
print("a equals 3")
Relational operators all perform the same task of specifying a relation in an if statement, that is then evaluated to be true or false
For example, to check if a is equal to 3, you use...
a is greater than or equal to 4
Output for the following:
a = 5
if a < 4:
print("a is less than 4")
else:
print("a is greater than or equal to 4")
If the "if" is true
The only time the "else" block won't execute
a = 5
if 4 <= a <= 10:
print("a is between 4 and 10")
else:
print("a is < 4 or > 10")
Print "a is between 4 and 10" or "a is < 4 or > 10" when a equals 5.
loops
A control structure for controlling the path through the code called ______. There are often times in our programs when we want to do something over and over and over until some condition is met.
x = 0
while x < 5:
x = x + 1
print(x)
Print x as "x+1" as long as x is less than 5.
while
Think of ______ as a repeating "if."
while my_str != "d" and my_str != "g":
print("not a valid selection")
Print "not a valid selection" if my_str is unequal to d and g.
import random
number = random.randint(0,100)
guess = int(input("Guess a number between 0 and 100: "))
Import Python's random number generator. Generate a random number, then prompt the user to guess a number between 0 and 100.
Import Python's random number generator. Generate a random number, then prompt the user to guess a number between 0 and 100.
If the guess was too high or too low, tell them, and prompt them to guess again. If the guess was correct, tell them, "that's it. the number was ___."
Explain the steps below:
import random
number = random.randint(0,100)
guess = int(input("Guess a number between 0 and 100: "))
numFound = False
while numFound == False:
if guess > number:
print("You guessed too high")
guess = int(input("Guess again: "))
elif guess < number:
print("You guessed too low")
guess = int(input("Guess again: "))
else:
print("That's it. The number was ", number)
numFound = True
iterator
A type of loop in Python that uses the keyword "for."
range
A type of collection that is a sequence of numbers, such as 1,2,3,4,5.
numbers = range(1,6)
Generate the sequence of numbers 1,2,3,4,5.
for x in range(1,6):
print(x * 2)
Multiply each number ("x") within the given range (of 1 to 5) by 2.
myList = ['a', 2, 'puppies', 4]
for item in myList:
print(item)
One of the other collection types we saw was a list. In a list, it's a collection of items that can be of different types and we can iterate over the list in the same way that we iterate over a string or a sequence
of numbers.
For example...