Python - Lists, For Loop
Python - Lists, For Loop
Lists, Loops
Practice Problems
P11) Check if a number is positive, negative, or zero.
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
days_rented = int(input("Enter the number of days the car was rented: "))
daily_rate = float(input("Enter the daily rental rate: "))
is_damage = input("Is the car damaged upon return? (Y/N)")
if days_rented <= 0:
print("Invalid input. Number of days should be greater than 0.")
else:
total_cost = days_rented * daily_rate
print("Total cost of car rental:", total_cost)
if str.upper(is_damage) == 'Y':
print("There is damage in the car upon return")
adjusted_amount = total_cost + 0.25*total_cost
print("Adjusted amount: ",adjusted_amount)
else:
print("There is no further adjustment needed in the amount.")
Python Lists
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3
are Tuple, Set, and Dictionary, all with different qualities and usage.
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
When we say that lists are ordered, it means that the items have a defined order, and that order
will not change.
If you add new items to a list, the new items will be placed at the end of the list.
To determine how many items a list has, use the len() function:
Accessing a List
List items are indexed and you can access them by referring to the index number:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
This will print "banana".
Negative indexing means start from the end. -1 refers to the last item, -2 refers to the second last item
etc.
You can specify a range of indexes by specifying where to start and where to end the range. When
specifying a range, the return value will be a new list with the specified items.
thislist =
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a
string).
This is less like the for keyword in other programming languages, and works more like an iterator
method as found in other object-orientated programming languages.
With the for loop, we can execute a set of statements, once for each item in a list, tuple, set etc.
With the break statement we can stop the loop before it has looped through all the items:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
With the continue statement we can stop the current iteration of the loop, and continue with the next:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1
(by default), and ends at a specified number.
for x in range(6):
print(x)
range(6) is not the values of 0 to 6, but the values 0 to 5.
The range() function defaults to 0 as a starting value, however it is possible to specify the starting
value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6):
list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
common_elements = []
fibonacci = [0, 1]
if n == 0:
exit()
if n == 1:
print("Fibonacci sequence:", fibonacci[0])
else:
print("Fibonacci sequence:", fibonacci)
if number < 2:
print(number, "is not a prime number.")
else:
is_prime = True
for i in range(2, number):
if number % i == 0:
is_prime = False
break
if is_prime:
print(number, "is a prime number.")
else:
print(number, "is not a prime number.")