Basics Flashcards
(107 cards)
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”)