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)
Can you add values of different types?
No you can’t lmao, you can add strings and numbers by having a string integer value
What are other ways to express multiplication
You can use parenthesis (3)(6), or just have variables times numbers or variables times variables by putting them right next to each other
4xy
xy
So when using -x and +x, and int(variableName) and float(variableName), there is only a return type, and the original values of the variable are not changed, so it’s easy to store a value inside of a variable
K
How to use the format specifier?
You know how when you are printing a variable, it is like
print(“This is the value”, value1)
And there is a space automatically right next to the string
You use the format specifier in the same way like
printformat((1234567.8973, ‘.2f’))
(the print value is an integer, it is not covered in strings, otherwise it won’t work
this will print out
1234567.89
You will have to use the format function btw to do this
How do you add commas to what you want to print?
You would do:
print(format(123456.8973, ‘,.2f’))
this would print out
123,56.89
You can also do this without precision
print(format(1234567.34567, ‘,f’))
would print out
1,234,567.34567
It is usually six digits printed after the decimal point that is the default
ok
Review width specifer
Ok
Let’s say a value is equal to 123456.78910, how would you print out The value is 123,456.7
value1 = 123456.78910
print(“The value is”, format(value1, ‘,.1f))
Actually, the .f rounds up, so it have an ending value of 8 instead of 7
How do you use the format method for integers, not decimals?
if you were to print out
print(“The value is” , format(1234567, ‘10,d’))
It would print out
The value is (two spaces(one is already there due to the comma))1,234,567
The comma counts as a placement
Ok
There is always a maximum of six digits after the decimal place
OK
What happens if you include percentage sign with this?
print(format(0.5, ‘.2%’))
would print out
%50.00
How to have no newline when printing out values?
Answers the practice questions from Chat GPT
ok
How does an if statement work?
An if statement works by having a boolean statement, and if it’s true the statements below will run, otherwise the decision statement will be skipped itself
In Python, for an if statement you do not need brackets, instead you need a colon right after the parenthesis, what is an example below of this?
If (value1 > 100):
value2 = value1
value = value1 + 1
What are some operational things for boolean?
== means if it’s equal
if (x <= y):
#statements
if(x >= y):
#statements
if(x != y):
#statements
You can also use the not operator in Python, what is an example of this?
if not(value1 < 100):
print(“Value1 is greater than or equal to 100”)
#essentially, you just reverse whatever is inside of the parenthesis
Instead of saying && and ||, we use and and or to say those for boolean operations
OK
How do you format if else statements?
You do:
if condition:
#statements
else:
#instead these statements will run if the boolean statement above is false
What are nested if statements?
It’s when you have if and else statements within each other, if you want to do the grade thing using nested statements, you would do the code
What are elif statements?
This is in replacement of nested if statements, and essentially means (else if)
You can also do comparison with strings, for example:
string1 = “Value1”
string2 = “Value1”
if (string1 == string2):
print(“These two variables are equal to each other)
else:
print(“There are not equal”)
How are string variables stored in Python?
They are stored by ASCII (this is on the cheat sheet, along with the format thing you have to answer GPT questions to make sure you memorized those)
How does randint and randrange work?
You would have to code “import random” at the top of your code in order to include the random module. So if you do that and do
value1 = random.randint(1, 6)
this includes both 1 and 6, whereas if you do randrange
value1 = random.randrange(1, 6)
it’s between 1 and 5(the end point is not inclusive)
You can also do this with randrange
random.randrange(0, 101, 10)
This takes values at intervals but never reaches or goes past 101(the default intervals is by 1s)
so 0, 10, 20, 30, 40… 100(doesn’t go past this)
random.randrange(10)
1, 2, 3, 4, 5, 6, 7, 8, 9(doesn’t go past this)
So just to review, to access the random module, you would do
import random
and randrange does include the starting value but not the ending value, while randint includes both, and you can also do this with randrange(10), which automatically has the intervals from 1 to 9, and you can also do randrange(1, 105, 5), which gets values 1, 6, 11, …(but if you want to get values in between 0-100, you would have to do randrange(0, 105, 5)
The starting value is always 0, btw, not 1
Ok lmao
What are the two types of loops in Python?
Condition controlled while loop and counter controlled for loop
How is a while loop formatted?
There is also a colon after the statement (like an if statement)
while value1 < 10:
#statements
What are some shortcuts that you can do for operations?
x+=5 = x = x + 5
y-=2 = y = y - 2
z*=10 = z = z * 10
a/=2 = a = a / 2
c%=3 = c = c % 3
(usually it is backwards from what you think from Java)
How would you format for loops?
You do
for i in range(5):
#statements
basically, i starts at 0, and goes through intervals of 1, and so once the first series of statements goes through, the value of i increases by 1 so it is now equal to 1, and the next series of statements runs (it never hits the range)
What is another way you can format a for loop?
You can use brackets, as you can do:
for value in [1, 2, 3, 4, 5]
value will first start with equaling to 1, and as the for loop goes through it increases by 1, you can also not do it in order, so it’s like
for value in [1, 6, 5, 3]
What is an easy way to memorize the maximum range?
For randrange and for i in range, it starts at 0, and then it NEVER hits the value(it is either 1 away or something like that based on the intervals)
So the two ways to format for loops are
for num in [(number list)]:
and also
for i in range(5):
i starts at 0, and goes through 1, 2, 3 and then 4(never reaches 5, similar to randrange)
You can also adjust the value of for i in range, like this (for i in range(1, 5), still it will never hit 5)
ok
You can also adjust how the intervals are, how do you do this
for i in range(1, 10, 5)
in this case, it will start at 1, then go to 6, but won’t hit 11
\t is essentially a tab, and it creates a bigger space than normal
ok
print() essentially just creates a newline
ok
So essentially, randint and for num in [(number list)] includes all of the numbers, and randrange and for i in range(value) does not include all of them(does not include the ending point
randrange(0, 101, 5)
this doesn’t include 101
for i in range(5) (0, 1, 2, 3, 4)
OKOK