Python + Flashcards
Use _________ to determine which operations are performed first
Parenthesis
Computers can’t store ______ completely accurately, which can lead to bugs.
Floats
How do you include a character that can’t be directly included in a string? Ex: a double quote
Use a backslash
How do you prompt the user for input?
input(‘prompt’)
User input is automatically returned as a ______. (With the contents automatically escaped.)
String
As with integers and floats, _______ in Python can be added, using a process called __________.
strings, concatenation
To add a string that contains a number to an integer you must first __________________.
Convert the string to an integer
You can multiply a string by a float. (T or F)
False
what is the output of this code:
print(3 * ‘7’)
777
How do you convert the string “83” into the integer 83?
int(“83”)
How do you convert the integer 7 into the float 7.0?
float(7)
What is the output of this code and why? \n\n\nint(‘6’+’3’)
63 , because the operation inside the parenthesis is done first and while ‘6’ and ‘3’ are inside the parenthesis they are strings so their sum is ‘63’, which is THEN converted into the integer 63
How do you convert user input (which is automatically a string) into an integer?
int(input(“prompt:”))
Variables can be reassigned as many times as you want. (T or F)
True
You can assign a variable to an integer and then later assign that same variable to a string. (T or F)
True
The only characters allowed in a variable name are:
Letters, Numbers, Underscores
is 13_Hab an acceptable variable name?
No, variable names cannot begin with numbers
Variable names are _____ sensitive.
case
How do you delete a variable?
del variable name
You can also take the value of the variable from user input. (T or F)
True
In–place operators allow you to write code like \n’x = x + 3’ more concisely, as:
x + = 3
What is the output of this code?
x = 3
num = 17
print(num % x)
2 (remainder)
What is the Boolean equal operator in python?
==
What is this Boolean operator? !=
Not equal to
Is this a true or false statement?\n\n\n1 != 1
False
Is this a true or false statement?\n\n\n2 != t
True
What are the Boolean operators for greater than / less than?
>
What are these Boolean operators?
<= >=
Greater than / less than (or equal to)
You can use an __ ________ to run code if a certain condition holds
if statement
Python uses ______ to delimit blocks of code
Indentation
The end of an if statement should contain a _.
:
If statements can be _______ to perform more complex checks. This is one way to see if ______________ are met.
nested, multiple conditions
What is the output of this code?\nnum = 7\nif num > 3: \n print(“3”) \n if num < 5: \n print(“5”) \n if num ==7: \n print(“7”)
3, because since 7 is not less than 5 the next nested statement is never reached
An else statement follows __________.
An if statement
An else statement contains code that is called when _____________________.
The if statement evaluates to false
(T or F)
Else statements must be followed by a :
True
The _____ statement is a shortcut to chaining if else statements.
elif
The and operator takes two arguments and evaluates as true if _________.
both arguments are true
The or operator takes two arguments and evaluates to true if ______________.
Either (or both) arguments is/are true.
Each character inside of a sting has a number, this number is called the _____
index
what is the output of:\n\n\n’python’[2]
t (always begin counting from 0)
what does the string function .len() do?
Gives you the number of characters in the string.\n(length)
write a line of code that gets rid of all capitalization in the variable named parrot
parrot.lower()
What will this line of code do?”parrot”.upper()
Make all letters in the string “parrot” uppercase
str() turns _______ into ________
non–strings into strings
Methods using dot notation (such as “String”.upper() ) only work with _________.
Strings
T or F) \n \nWhen you want to print a variable inside of a string, the best way is concatenation
False
(name = “Eric” age = 74)
f”Hello, {name}. You are {age}.”)
use the % operator to replace the__ placeholders with the variables in parentheses.
%s
According to operator precedence what is the output of this code? if 1 + 1 * 3 == 6: print("Yes") else: print("No")
No
What are the 5 basic categories of statements that programming languages have.
Conditional Looping Function Assignment Declaration
______ = 5.0 / 3.0 * 6.0
10.0
When a function is executed, what happens when the end of the function statement block is reached?
Execution returns to the statement that called it.
For a function to be used in an expression, it must _____________ .
Return a Value
A __________ is the part of a program in which a variable may be accessed.
Scope
while in a loop, a _____________ command allows to to exit before the completion of the loop
Break
A simple conditional statement taught in this course starts with the key word _____ .
if
What are two keywords used to start loop (repeating) statements in Python?
For and While
A loop that repeats a specific number of times is know as a(n)_________________.
Count controlled loop
What value will be printed after this code in run?
x = 12\ny = 10\ny += 15
myvar = x + y
print myvar
37
______________ gives us this ability to choose among outcomes based off what else is happening in the program.
Control flow
evaluate
not 41>40
False
evaluate
not 65 < 50
True
evaluate
not not 50 != 50
False
What is the order of operations for boolean operators?
not
and
or
(in?)
an __/_____ statement says “If this expression is true, run this indented code block; otherwise, run this code after the else statement.”
if/else
use _________ to determine if a string is letters.
STRING.isalpha()
Create a line of code that checks if user input has more than 0 characters AND is all letters
if len(INPUT) > 0 and INPUT.isalpha()
when selecting a section of a string that should include the end, for the second index you can use .len() or you can ______.
Leave second index blank
How do you use pi?
from math import pi
How do you use the sleep function?
from time import sleep
Write some code that will print the current date and time
from datetime import datetime\nnow = datetime.now()\nprint now
How do you print a float to the second decimal place using string formatting?
%.2f
How do you insert a float using string formatting?
%f
What are the 3 general parts of a function?
Header (includes ‘def’)
Comment (optional)
body (indented)
what is n? def square(n):
a parameter, which acts as a variable name for a passed in argument.
T or F You can call another function from within a function
True
What does this code do? def cube(number): return number**3 def by_three(number): if number % 3 == 0: return cube(number) else: return False
when cube is called it will return the cube of the number.\n\n\nwhen by_three is called it will return the number cubed IF the number is divisible by three
Write a line of code that prints the square root of 25 using built in functions
import math
print math.sqrt(25)
How do you import just one function from a module?
from module import function
What if you want all of the math functions but don’t want to keep typing in ‘ math. ‘ before calling each function, what can be done?
rom math import *\n(Universal import)
Why is it not always a good idea to use a universal import?
if you have functions named the same as the functions in the module it can confuse the program.