W2 - Boolean, Precedence, Intro to Sequences, Type Casting Flashcards
What are True and False equal to?
1 and 0 respectively
What is the return type for the expression 4 == x? What are the possible values?
bool, and either True of False (CAPITAL FIRST LETTERS)
What will this code yield?
»> myval = 3 < 5
»> myval + 7
_______?
8, the boolean True is equivalent to a 1
what are the and/or operators?
literally just the words “and” and “or”
order of precedence for operators (highest = top, lowest = bottom)
parentheses
exponentiation
negation
multiplication/division (including modulus)
addition/subtraction
relations (<, >, <=, ==, !=, etc)
not
and
or (nandor!)
define associativity
same precedence, therefore move left to right (like PEMDAS)
what are the logical operators?
not, and, or (nandor)
strings returned with any quotation marks around them are returned with (double/single) marks
single
Show the result:
»> myname = ‘monty’
»> len(myname)
5
Show the result:
»> myname = ‘monty’
»> myname[0]
would result in ‘m’ (with the single quotation marks)
Show the result:
»> 3 in [44, 3, 5, -9]
True (rmr, capital letters!)
when should you use snake case?
when describing a unit of a variable in the variable name (ex. timeSinceStarted_ms)
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
2 and 4 are correct
True/false: Strings can be modified
false, they are immutable
are lists mutable?
yes!
what are sequences?
a type of data structure that orders data in a specific sequence, and can be indexed with integer indices
true/false: python shows strings with single or double quotes depending on the data type of the output
false, python ALWAYS shows strings with single quotes unless printed, in which case there are no quotes
how would you typecast to:
1. a string
2. a float
3. an integer
4. a boolean
- str()
- float()
- int()
- bool()
what is the value of True? what about False?
False == 0
True is 1 generally, except when converted from a number to a booolean (i.e. bool(4)) which will return True
what will int(5.7) return?
5, float —-> int causes the decimal to be truncated
what will float(7//2) return?
3.0, returns an integer that is ONLY AFTER COMPLETION turned into a float
mixedList = [22, ‘hello’, 4>6]
what will print(mixedList) result in?
[22, ‘hello’, False]
(results, not the equations are stored!)
how do you print with quotations?
use the repr() function
print(‘word’, repr(‘word’))
will result in
word ‘word’
abd > abcd
abcd > abc
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
how do you remove a already imported library from code?
restart the code environment (restart session in jupyter notebooks)
import the choice function from the random library
from random import choice
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()
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
what does randint(a, b) do?
creates a random integer between a and b (inclusive)
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
what does
x = “False”
bool(x) == False
return?
false, you cannot typecast a string to anything but true unless the string is empty (“”)
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
How to find last digit of 125? (easiest way)
125%10
random vs. random.random()
random is a module, random.random() is a function within that module