Week 1 - Intro to Python, Variables, Built-in Functions, Number Types Flashcards
Which will perform the intended function?
if 5 > 2:
l print(“Five is greater than two!”)
OR
if 5 > 2:
print(“Five is greater than two!”)
The first line of code, as the indentation indicates to python that the print() line is part of the if statement. (indentation matters!)
If you execute the code (in a google copilot text cell):
4+6
1+2
which will be returned?
3
what is the result of:
> > > 2e3
2000.0
remember, e always creates a float value!
how would you make an expression over multiple lines?
use either \ OR ()
»>45 + 39 \
… -3
> > > 45 + 93 \
-4
> > > (4293 + 0394923948
+323)
all work!
what happens if you name a variable a function name (i.e. print = 4)
the variable will work, but the function will be unusable until you delete the variable using “»> del print”
does this work?
x, y = 2, 3
yes
how does the round function work?
it rounds up or down unless the decimal is 0.5, in which it rounds up if the whole number is odd, and down when the whole number is even.
x = 5
y = 12
what will
print(x, y)
result in? what about
x, y
?
print will give you
5 12
nonprint will give you
(5, 12)
^creates an object (not in scope of lesson 1)
what happens when a function isnt returning anything, but are run?
functions like print actually return the value “None”
what does the type() function do?
returns the types of variables (which are actually objects created from classes)
type shows the type, or CLASS, of a variable, or OBJECT
print(type(x))
<class ‘int’>
what are the types of numbers in Python?
int, float, and complex (a + bj or a + bJ)
print(type(3)) VS type(3)
<class ‘int’>
vs
int
addition, subtraction, multiplication, negation, and exponentiation
*
-
**
float division, integer division, remainder division
float = /, always results in float
integer = //, results in float only if float as numerators/denominators, but CONCEPTUALLY DIVIDES INTO INTEGER (4.5//2 = 2.0)
remainder = modulus = % = remainder left over (could be either float or int)
round 2/3 to 3 decimal places
round(2/3, 3)