list1 = [34, 59, 61, 23, 43]
for n in list1:
print(n)
# Output:
34
59
61
23
43
or
for n in list1:
print(n, end=" -- ")
# Output:
34 -- 59 -- 61 -- 23 -- 43 --
You can also print a list directly like this:print(list1) # Output: [34, 59, 61, 23, 43]
print(list1[3]) # Output: 23This result is explained by this table:
| list1 | Index | 0 | 1 | 2 | 3 | 4 |
| Value | 34 | 59 | 61 | 23 | 43 |
for n in range(0, len(list1):
print(n, end="; ")
# Output:
34; 59; 61; 23; 43;
list2 = [154, "dog", 3.14159, True]
for value in list2:
print(value)
# Output:
154
dog
3.14159
True
for value in list2:
print(type(value))
# Output:
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>
f f_5 find_record update_vehicle_miles
f f5 findRecord updateVehicleMiles
Write user and test defined methods. The method header is shown first with the description on subsequent lines.
# Ans:
def print_greeting( ):
print("Hello, how are you?")
print_greeting( )
# Output:
Hello, how are you?
def print_greeting(the_person):
print(f"Hello, {the_person}, how are you?")
print_greeting("Alice")
# Output:
Hello, Alice, how are you?
# Ans:
def get_greeting(the_name):
return "Hello, " + the_name + ", how are you?"
print(get_greeting("Alice"))
# Ans:
Hello, Alice, how are you?
# Ans:
def to_meter(feet, inches):
m = (feet * 12.0 + inches) * 2.54 / 100.0
return m
print(to_meter(2, 3)
# Output: 0.6912
def to_feet_inches(meters): f = meters * 100.0 / (2.54 * 12.0) feet = int(f) inches = int(round((f - feet) * 12.0, 0)) output = str(feet) + "' " + str(inches) + '"' return output print(to_feet_inches(0.6912))
# Output: 2' 3"