Basics Flashcards
Print hello world!
print(“hello world!”)
Two ways to indent
Tab
Four spaces
Make 3 into a float and assign to prob
prob = float(3)
What is the type of test in the below?
test = 3
int
Why use double quotes over single quotes?
If you want to use an apostrophe or single quote. Otherwise, preference
Define a string named mystring with value hello.
mystring = “hello”
Assign mystring value:
Don’t worry about apostrophes.
mystring = “Don’t worry about apostrophes.”
Concatenate the below two variables into helloworld:
hello = “hello”
world = “world”
helloworld = hello + world
assign 3 and 4 to a and b respectively (one line)
a, b = 3, 4
what will happen in the below code?
one = 1
two = 2
hello = “hello”
print(one + two + hello)
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
what is a list?
a type of array that can contain any type of variable and as many variables as needed.
what will be the output of the below? mylist = [] mylist.append(1) mylist.append(2) mylist.append(3) print(mylist)
[1, 2, 3]
what will be the output of the below:
mylist = [1,2,3]
print(mylist[10])
IndexError: list index out of range
what is the output of the below (remember PEMDAS):
number = 1 + 2 * 3 / 4.0
print(number)
2.5
what is the % operator do?
returns remainders
what is the output of:
remainder = 11 % 3
print(remainder)
2
Assign 2^3 to the variable ‘cubed’
cubed = 2 ** 3
what is the output here:
helloworld = “hello” + “ “ + “world”
print(helloworld)
hello world
assign “hellohellohellohello” to lottahello using the string hello and an operator.
lottahello = “hello” * 3
OR
lottahello = “hello” + “hello” + “hello”
what would be the output of the below: even_numbers = [2,4,6,8] odd_numbers = [1,3,5,7] all_numbers = odd_numbers + even_numbers print(all_numbers)
[1, 3, 5, 7, 2, 4, 6, 8]
what would be the output of the below:
print([1,2,3] * 3)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
what are two methods in formatting strings? provide an example of each.
two types:
1. c type:
%s - String (or any object with a string representation, like numbers)
%d - Integers
%f - Floating point numbers
%.f - Floating point numbers with a fixed amount of digits to the right of the dot.
%x/%X - Integers in hex representation (lowercase/uppercase)
- str.format()
name = “Amit”
print(“hello, %s!”, name)
print(“hello, {}”.format(name))
what is the output here:
astring = “Hello world!”
print(astring.index(“o”))
4, looks for the first “o”. Indexing starts at 0, not 1.
Count how many l’s there are in variable “astring”.
astring = “Hello world!”
astring.count(“l”)
how do you index a string based on a value? say the value you are looking for is “a”
str.index(“a”)
print the “o” in astring from “hello” using []:
astring = “Hello world!”
print(astring[4])
given str[start:stop:step], how can you print every other value from astring = “splicing strings” starting at “l”? What would the output be?
print(astring[2::2])
output: lcn tig
given astring = “Hello world!”, what value for the “step” portion of splicing would produce the same output as below:
print(astring[3:7])
ans = 1
see modified print statement below:
print(astring[3:7:1])
using astring = “Hello world!”, print the reverse of this.
print(astring[::-1])
remember, str[start:stop:step]
Assign astring variable to another variable called astringUpper and make the entire string all upper case
astringUpper = astring.upper()
Assign astring variable to another variable called astringLower and make the entire string all lower case
astringLower = astring.lower()
How can you check if astring starts with “hello”?
print(astring.startswith(“hello”))
if True, will print True
How can you check if astring ends with “orld!”?
print(astring.endswith(“orld!”))
if True, will print True
Split astring = “Hello world!” on the space. What would the output be?
print(astring.split(“ “))
output: [‘Hello’, ‘world!’]
how can you check if variable a equals 3?
a == 3
how can you check if variable a is not equals 3?
a != 3
“and” and “or” are operators – true or false?
true
“in” cannot be used to check an iterable object – true or false?
false, “in” operator could be used to check if a specified object exists within an iterable object container, such as a list
A statement is evaluated as true if one of the following is correct: – 2 reason, list them
- The “True” boolean variable is given, or calculated using an expression, such as an arithmetic comparison. 2. An object which is not considered “empty” is passed.
Give at least 4 examples for objects which are considered as empty: – list them
- An empty string: “” 2. An empty list: [] 3. The number zero: 0 4. The false boolean variable: False
x = [1,2,3]
y = [1,2,3]
Why does this print out False?
print(x is y)
Unlike the double equals operator “==”, the “is” operator does not match the values of the variables, but the instances themselves
what does using “not” before a boolean do? give two examples (one for each boolean)
it inverts it.
print(not False) # Prints out True
print((not False) == (False)) # Prints out False