W2 - Boolean, Precedence, Intro to Sequences, Type Casting Flashcards

1
Q

What are True and False equal to?

A

1 and 0 respectively

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

What is the return type for the expression 4 == x? What are the possible values?

A

bool, and either True of False (CAPITAL FIRST LETTERS)

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

What will this code yield?
»> myval = 3 < 5
»> myval + 7
_______?

A

8, the boolean True is equivalent to a 1

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

what are the and/or operators?

A

literally just the words “and” and “or”

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

order of precedence for operators (highest = top, lowest = bottom)

A

parentheses
exponentiation
negation
multiplication/division (including modulus)
addition/subtraction
relations (<, >, <=, ==, !=, etc)
not
and
or (nandor!)

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

define associativity

A

same precedence, therefore move left to right (like PEMDAS)

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

what are the logical operators?

A

not, and, or (nandor)

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

strings returned with any quotation marks around them are returned with (double/single) marks

A

single

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

Show the result:
»> myname = ‘monty’
»> len(myname)

A

5

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

Show the result:
»> myname = ‘monty’
»> myname[0]

A

would result in ‘m’ (with the single quotation marks)

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

Show the result:
»> 3 in [44, 3, 5, -9]

A

True (rmr, capital letters!)

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

when should you use snake case?

A

when describing a unit of a variable in the variable name (ex. timeSinceStarted_ms)

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

Which of the following are true?
1. the = operator is used for both assignments and equality
2. Strings can be compared using the relational operators
3. The first character in a string variable mystring is mystring[1]
4. The len function can be used for both strings and lists

A

2 and 4 are correct

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

True/false: Strings can be modified

A

false, they are immutable

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

are lists mutable?

A

yes!

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

what are sequences?

A

a type of data structure that orders data in a specific sequence, and can be indexed with integer indices

16
Q

true/false: python shows strings with single or double quotes depending on the data type of the output

A

false, python ALWAYS shows strings with single quotes unless printed, in which case there are no quotes

17
Q

how would you typecast to:
1. a string
2. a float
3. an integer
4. a boolean

A
  1. str()
  2. float()
  3. int()
  4. bool()
18
Q

what is the value of True? what about False?

A

False == 0
True is 1 generally, except when converted from a number to a booolean (i.e. bool(4)) which will return True

19
Q

what will int(5.7) return?

A

5, float —-> int causes the decimal to be truncated

20
Q

what will float(7//2) return?

A

3.0, returns an integer that is ONLY AFTER COMPLETION turned into a float

21
Q

mixedList = [22, ‘hello’, 4>6]
what will print(mixedList) result in?

A

[22, ‘hello’, False]

(results, not the equations are stored!)

22
Q

how do you print with quotations?

A

use the repr() function

print(‘word’, repr(‘word’))
will result in
word ‘word’

23
Q

abd > abcd
abcd > abc
a > A

A

True, d comes after c and is therefore greater
True, abcd is longer than abc despite having the same first 3 letters
True, lowercase > uppercase

24
how do you remove a already imported library from code?
restart the code environment (restart session in jupyter notebooks)
25
import the choice function from the random library
from random import choice
26
rename the random library to bob, and choice to bill
import random as bob now you can do bob.random() from bob import choice as bill now you can do bob.bill()
27
what does .upper() or .lower() do to a string?
nothing, strings are immutable, thus an external function cannot alter it. only reassignment can change what you get when you print the variable connected to the mem. location
28
what does randint(a, b) do?
creates a random integer between a and b (inclusive)
29
what does choice do?
chooses a random ITEM IN A SEQUENCE! CANNOT BE USED LIKE RANDIN UNLESS THE SEQUENCE IS LITERALLY A LIST OF NUMBERS/STRING OF NUMBERS
30
what does x = "False" bool(x) == False return?
false, you cannot typecast a string to anything but true unless the string is empty ("")
31
function vs. method
function = nonspecific to a single class, whereas method is an action defined in the class thus function = no dot operator, whereas method does
32
How to find last digit of 125? (easiest way)
125%10
33
random vs. random.random()
random is a module, random.random() is a function within that module
34