week1 Mon

week1 Mon#

# what is 
# (1) int
# (2) float
type(3)

# every thing in python has a type
int
# what is 
# (1) 3.5
# (2) 3
7/2

# / is "true division". It always returns a float.
3.5
# what is 
# (1) 3.5
# (2) 3
7//2

# // is "floor division". It always returns an integer.
3
7%2
# % is the modulo operator. It returns the remainder of the division of the number to the left by the number on its right.
1
# T or F
# (1) True
# (2) False
3 - 2 == 1 
True
# T or F
# (1) True
# (2) False
0.8 - 0.7 == 0.1

# floating point numbers are not stored with perfect precision. This can lead to small errors in calculations.
False
#  what is printed
# (1) 0,1,2,3
# (2) 0,1,2
# (3) 1,2,3
for i in range(3):
    print(i)

#  range(start[default=0], stop, step[default=1])
0
1
2
# what is the output
# (1) 4, 8
# (2) 4, 8, 16
x = 2
while x < 10:
    x = x*2
    print(x)
4
8
16
# what is x[7]
# (1) 0
# (2) error 
x = [0, 1 ,2 ,3, 4 ,5]
x[-7]

# python uses 0-based indexing, so the first element of a list has index 0, the second element has index 1, and so forth.
# the last element of a list has index -1, the second to last element has index -2, and so forth.
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/var/folders/3b/sldzpq7d6h9gbk6l26n1g20w0000gn/T/ipykernel_51478/503096209.py in <module>
      3 # (2) error
      4 x = [0, 1 ,2 ,3, 4 ,5]
----> 5 x[-7]

IndexError: list index out of range
# x[start(inclusive, default 0): end(exclude, default -1): step (default 1)]
# what is the output
x = [0, 1 ,2 ,3, 4 ,5]
for i in x[2:]:
    print(i)
2
3
4
5
# what is the output
# (1) True
# (2) False
"hello" == 'hello'

# single quotes and double quotes can both be used to create strings. Though try to be consistent in your use of quotes.
True
type("string")
str
type('string')
str
# is this valid
# (1) Yes
# (2) False
x = [True, 1,'hello']

# lists can contain elements of different types.
type(x[0])
bool
type(x[1])
int