CS 10 Test #2 Notes Flashcards
How do quotes work for python
You can do print(“value”) or print(‘value’) (no semicolons)
If you want to print ‘Hello’, you cannot do it like print(‘ ‘Hello’ ‘) because Python thinks that from ‘ to ‘, is what is only being printed
How to use comments?
You can just do #
How do you assign a value in Python?
You can just do variableName = value, no need for semicolon or putting the type of the variable
How do you reassign values?
Lets say you declare a value
value3 = 3
#You have to declare what a variable is equal to in python
and then if value1 = 6, you can reassign the value
value1 = value3
now value1 is equal to 3
print(value1)
USE GPT FOR PRACTICE QUESTIONS ON EVERYTHING YOU LEARNED
ok
Are Python values fixed?
No they are not. For example, if you have a value
value1 = 3
you can reassign the value with a string
value1 = “hello”
and print it out that way
How do you do mathematical operations in Python?
You just do it normally, so for example:
total = 0
value1 = 2
value2 = 3
total = value1 + value2
average = 0
average = (value1 + value2)/2
What happens if you do 10 = num
This is a syntax error
How do you output values with strings?
you can do print(“This is value1”, value1)
assume value1 is equal to 3, and there’s automatically a space between the two
Is there always a newline between the print statements?
Yes there is
How do you take a users input?
You do
value1 = int(input(“What is a value that you want to input?))
Also, it is always automatically a string value unless you don’t specificy the type(no matter whether it’s letters or integers)
what does int() do?
It just rounds down a number(if it’s a decimal)
What can’t you name your variables in Python?
The general rules are:
It can’t contain spaces
It can’t have numbers at the start(must have lowercase letters or uppercase letters, or underscores)
what is the difference between a float and an integer value?
a float is a decimal whereas an integer value is a whole number
What are different operations that you can do in Python for a variable?
You can do addition, subtration, multiplication, for division it actually depends on the type, as you can round it down if you do //, but you can keep it a decimal if you do /. For exponents, you do the * sign twice, and for remainder it’s %
% means mod, returns the remainder of a value
for example:
value1 = 3;
value1 = 8 % 3
#value1 is now equal to 2
OK BRO
// is integer division, / is float division
ok, there’s always a float number that is returned, and for integer division is it always rounded down(not up)
3 / 4 / 2 is equal to 0.75 / 2 equal to 0.375, whereas exponents are solved from right to left. like this 2 ** 2 ** 4 is not 256, it’s 2 ** 8 = 256
ok
3 + 6 * 5 * 5 ** 2 ** 3
3 + 6 * 5 * 5 ** 8
3 + 6 * 5 * 390625
11718750 + 3
11718753
ok
what does +x and -x mean
if x is a negative value, +x makes it positive (BUT DOES NOT ALTER THE VALUE ITSELF, IT JUST HAS A RETURN TYPE OF POSITIVE)
You can also store this value into another variable
For example
What does ~x mean?
It inverses the binary value of the number (review binary stuff it’s legit CSP)
if you have a value:
x = 1
What does -x and +x called?
Unary minus and unary plus
What is integer division called?
Floor division
Review the second page of Lesson 6
OK
((year % 4) == 0 and (year % 100) != 0) or (year % 400 == 0)