CS 10 Test #2 Notes Flashcards

1
Q

How do quotes work for python

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to use comments?

A

You can just do #

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you assign a value in Python?

A

You can just do variableName = value, no need for semicolon or putting the type of the variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you reassign values?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

USE GPT FOR PRACTICE QUESTIONS ON EVERYTHING YOU LEARNED

A

ok

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are Python values fixed?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you do mathematical operations in Python?

A

You just do it normally, so for example:
total = 0
value1 = 2
value2 = 3
total = value1 + value2
average = 0
average = (value1 + value2)/2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if you do 10 = num

A

This is a syntax error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you output values with strings?

A

you can do print(“This is value1”, value1)
assume value1 is equal to 3, and there’s automatically a space between the two

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is there always a newline between the print statements?

A

Yes there is

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you take a users input?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what does int() do?

A

It just rounds down a number(if it’s a decimal)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What can’t you name your variables in Python?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the difference between a float and an integer value?

A

a float is a decimal whereas an integer value is a whole number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are different operations that you can do in Python for a variable?

A

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 %

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

% means mod, returns the remainder of a value

for example:
value1 = 3;
value1 = 8 % 3
#value1 is now equal to 2

A

OK BRO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

// is integer division, / is float division

A

ok, there’s always a float number that is returned, and for integer division is it always rounded down(not up)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

ok

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

3 + 6 * 5 * 5 ** 2 ** 3
3 + 6 * 5 * 5 ** 8
3 + 6 * 5 * 390625
11718750 + 3
11718753

A

ok

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what does +x and -x mean

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does ~x mean?

A

It inverses the binary value of the number (review binary stuff it’s legit CSP)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

if you have a value:
x = 1
What does -x and +x called?

A

Unary minus and unary plus

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is integer division called?

A

Floor division

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Review the second page of Lesson 6

A

OK

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

((year % 4) == 0 and (year % 100) != 0) or (year % 400 == 0)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Can you add values of different types?

A

No you can’t lmao, you can add strings and numbers by having a string integer value

27
Q

What are other ways to express multiplication

A

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

28
Q

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

A

K

28
Q

How to use the format specifier?

A

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

29
Q

How do you add commas to what you want to print?

A

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

30
Q

It is usually six digits printed after the decimal point that is the default

A

ok

31
Q

Review width specifer

A

Ok

32
Q

Let’s say a value is equal to 123456.78910, how would you print out The value is 123,456.7

A

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

33
Q

How do you use the format method for integers, not decimals?

A

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

34
Q

The comma counts as a placement

A

Ok

35
Q

There is always a maximum of six digits after the decimal place

A

OK

36
Q

What happens if you include percentage sign with this?

A

print(format(0.5, ‘.2%’))
would print out
%50.00

37
Q

How to have no newline when printing out values?

A
38
Q

Answers the practice questions from Chat GPT

A

ok

39
Q

How does an if statement work?

A

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

40
Q

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?

A

If (value1 > 100):

value2 = value1
value = value1 + 1

41
Q

What are some operational things for boolean?

A

== means if it’s equal
if (x <= y):
#statements

if(x >= y):
#statements

if(x != y):
#statements

42
Q

You can also use the not operator in Python, what is an example of this?

A

if not(value1 < 100):
print(“Value1 is greater than or equal to 100”)
#essentially, you just reverse whatever is inside of the parenthesis

43
Q

Instead of saying && and ||, we use and and or to say those for boolean operations

A

OK

44
Q

How do you format if else statements?

A

You do:
if condition:
#statements
else:
#instead these statements will run if the boolean statement above is false

45
Q

What are nested if statements?

A

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

46
Q

What are elif statements?

A

This is in replacement of nested if statements, and essentially means (else if)

47
Q

You can also do comparison with strings, for example:

A

string1 = “Value1”
string2 = “Value1”
if (string1 == string2):
print(“These two variables are equal to each other)
else:
print(“There are not equal”)

48
Q

How are string variables stored in Python?

A

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)

49
Q

How does randint and randrange work?

A

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)

50
Q

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

A

Ok lmao

51
Q

What are the two types of loops in Python?

A

Condition controlled while loop and counter controlled for loop

52
Q

How is a while loop formatted?

A

There is also a colon after the statement (like an if statement)
while value1 < 10:
#statements

53
Q

What are some shortcuts that you can do for operations?

A

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)

54
Q

How would you format for loops?

A

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)

55
Q

What is another way you can format a for loop?

A

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]

56
Q

What is an easy way to memorize the maximum range?

A

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)

57
Q

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)

A
58
Q

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)

A

ok

59
Q

You can also adjust how the intervals are, how do you do this

A

for i in range(1, 10, 5)

in this case, it will start at 1, then go to 6, but won’t hit 11

60
Q

\t is essentially a tab, and it creates a bigger space than normal

A

ok

61
Q

print() essentially just creates a newline

A

ok

62
Q

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)

A

OKOK

63
Q
A