WEEK 1 python Flashcards
3 + 3 = 6
what are the
values
operator
evaluation
3 = values
+ = operator
6 = evaluation
what is ‘’//’’
integrer division(floor division): 22/8 = 2.75 but we only take 2
integers
floating numbers
strings
integers (INT): whole numbers (-2, 0, 2)
floating numbers (FLOATS): numbers with decimals (-1.25, 0.5, 1.0)
Strings (STR): text values inside quotes: (‘Hello’, ‘11 cats’)
How does + (addition) work for numbers and strings
Numbers: adds them (2 + 3 = 5)
Strings: joins them together ‘John’ + ‘Bob’ –> JohnBob
How do you add a string and a number
(‘Alice’ + str(43)) –> Alice43
you have to convert the number to a string manually.
how does multiplication work for numbers and strings
Numbers: 4 * 5 = 20
Strings and numbers: ‘Alice’ * 6 –> AliceAliceAliceAliceAliceAliece (repeats the string X times)
you cannot multiply two string together
what does a variable do
stores a value in memory for later use, you create this by using ‘=’
spam = 40
from now on the variable ‘spam’ now holds 40
what is overwriting
if yo assign a new variable to a variable, the old value is lost.
valid variable names can only use
letters, numbers and underscores (_)
valid variable names are…
cannot start with a number, contain spaces/special characters (like (-, $, @), case sensitive, camelcase can be used (every new word starts with capital letter)
what is done with the #
nothing, python ignores it, these are just comments for yourself
What is the Interactive Shell?
A tool that lets you run Python ONE LINE AT A TIME and see results immediately (REPL).
What is the File Editor?
A tool for writing FULL Python programs, where code is saved in a .py file and run all at once.
What is the difference between the Interactive Shell and File Editor?
The Interactive Shell runs code instantly (»> prompt), while the File Editor saves and runs entire scripts.
Why doesn’t the File Editor run code when you press Enter?
Because it is used for writing full programs, not for executing commands line-by-line.
What happens when a Python program runs from a file?
Python executes the entire script and terminates when there are no more lines of code.
commenting out
when writing a program, you may want to temporarily disable a line of code without deleting it. Instead of removing the line, you can “comment it out” using #.
Functions do something when called.
In Python, when you write print(“Hello, world!”), you are calling the print() function.
What is input() used for?
input() allows the user to type information, making the program interactive.
What happens if you hardcode a value? (myName = “Alice”)
The program always uses “Alice”—it never changes, no matter who runs it.
Why use input() instead?
It lets any user enter their own name, making the program dynamic and interactive.
What is len() used for?
It counts the number of characters in a string, including spaces.
What is the output for len(“Alice”)?
5 (since “Alice” has 5 characters: A, l, i, c, e)
Can you convert a number to a string?
Yes! Use str(number) to turn a number into a string.
Can you convert a string to a number?
Yes! Use int(string) for whole numbers and float(string) for decimals, so only possible with numbers that are treated as words.
What does int() do?
Converts a decimal number or a string into an integer (whole number).
Example of int()?
int(1.99) → 1 (Rounds down!)
What does float() do?
Converts an integer or a string into a decimal (floating-point number).
Example of float()? –> float(10)
→ 10.0
Why does input() return a string?
input() always returns text, even if you enter a number
'’42’’ is it a number of a string
with ‘’ it is a string
is this correct 42 == ‘‘42’’
false
how can you change 42 == ‘‘42’’ to the correct version
42 == int( ‘‘42’’)
is this the same according to python: 42 == 42.0
yes
what does python do with extra zeros
they ignore them, so 042.000 == 42.0