Python Programming Revision
Python Programming Revision
Topics:
Loops can execute a block of code number of times until a certain condition is met. (or)
The iteration statement allows instructions to be executed until a certain condition is to be fulfilled. The
iteration statements are also called as loops or Looping statements.
✓ While loop
✓ For loop
body
Where, loop body contain the single statement or set of statements (compound statement) or an empty
statement.
The loop iterates while the expression evaluates to true, when expression becomes false the loop
terminates.
Example 1: to print the numbers from 0 to 5 using while loop as follows:
Count=0 Output
while(count<=5): 0
print(count) 1
2
count=count+1 3
4
5
5
6
7
8
9
10
Example 3: Multiplication table
***********************************************************************************
What is a List?
• Lists are used to store multiple items in a single variable (which is used to store collections of
data). Lists are created using square brackets.
Different types of list
• List items are indexed, the first item has index [0], the second item has index [1] etc.
• The first index is zero, the second index is one, and so forth.
List length
To determine how many items a list has, use the len() function as shown below:
Output
Here's a simple example of finding the highest value in a list using Python:
Output
45
For minimum value:
Output
4
Accessing Values in Lists
List items are indexed and you can access them by referring to the index number (using square brackets)
Example:
Output
chemistry
Negative Indexing
Example: If we want to print the last item of the list, the following code must be given:
Range of Indexes
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.
Output:
Range of Negative Indexes
Output :
Append Items
To add an item to the end of the list, use the append( ) method:
Example : After creating a list with how to add more items using append( )?
colors=[“blue”, “orange”]
colors.append(“red”)
colors.append(“green”)
print(colors)
2. Write a program that asks the user to enter five names. Store these names in a list called
"names". Then, print the names
3. names = []
4.
5. name1 = input("Enter name 1: ")
6. name2 = input("Enter name 2: ")
7. name3 = input("Enter name 3: ")
8. name4 = input("Enter name 4: ")
9. name5 = input("Enter name 5: ")
10.
11. names.append(name1)
12. names.append(name2)
13. names.append(name3)
14. names.append(name4)
names.append(name5)
print(names)
3. Write a program that asks the user to enter five names. Store these names in a list called
"names". Then, print the names in reverse order.
4. Create an empty list called ages[ ]. And add the ages 30,35,40,45,50,55,60 using append
function. Increase the value at index 2 by 3. Print the updated list.
5. Write a program that asks the user to enter a series of names. Store all the entered names in a
list called "names" in the list using append function. Finally, print the list of names. And also
print the index 2 value of the list.
6.Create an empty list called numbers[ ]. Write a PYTHON program to ask the user to enter 5 numbers
and using append function add those entered numbers in the list. Update the value of index 2 with new
value entered by user.
7. Create an empty list called “marks”. Write a PYTHON Program to ask the user to enter 5
subject marks and using append function add those marks in the list. Print the marks [ ] list and
also print the total marks of those 5 subjects.
Practice questions on Loops:
1. Write a python program to print first 10 natural numbers using while loop
2. Write a python program to print sum of 5 natural numbers using while loop
4. Write a python program to print even numbers upto 20 using while loop
5. Write a python program to print odd numbers from 1 to 10 using while loop
7. Write a python program to print table of a number entered from the user.
Year 8 - Lists in python Practice questions
1. Create a list called fruits with the elements "apple", "banana", and "orange". Use the insert()
function to add "grape" at index 1. Print the updated list.
2. Create an empty list called "colors" with the items "blue", "red", "orange", "yellow" and
"purple". Use the insert( ) function to add "Pink" at the index 3. Print the updated list
3. Create a list called names with the elements "Alice" and "Bob". Use the insert() function to add
"Charlie" at the beginning of the list. Print the updated list.
4. Create a list called letters with the elements "a", "b", and "c". Use the insert() function to add
"d" at index 2. Print the updated list
5. Create a list called animals with the elements "cat" and "dog". Use the append() function to add
"elephant" at the end of the list. Print the updated list.
6. Create a list called animals with the elements "cat" and "dog". Use the append() function to add
"elephant" at the end of the list. Print the updated list.
7. Create a list called numbers with the elements [1, 2, 3, 4, 5]. Change the value at index 2 to 10.
Print the updated list.
8. Create a list called fruits with the elements ["apple", "banana", "orange"]. Change the value at
index 1 to "grape". Print the updated list.
9. Create a list called names with the elements ["Alice", "Bob", "Charlie"]. Change the value at
index 0 to "David". Print the updated list.
10. Create a list called ages with the elements [25, 30, 35, 40]. Change the second index value 35 by
replacing it with 34 and 38 . Print the updated list.
1. Write a python program that uses a for loop to print first 10 Natural numbers (Eg:
1,2,3...10) answer please
2. Write a python program using for loop to print the numbers starting from 1 till 50.
3. Write a python program using for loop and range( ) function to print the number sequence
3,6,9,12,15 ......... 30.
4. Write a program that uses a for loop and the range() function to print all the even numbers
between 1 and 20
5. Write a program that uses a for loop and the range() function to print the multiples of 5 from 0
to 100.
6. Write the python program using for loop and range( ) function to print the
numbers 1 to 5 in reverse order.
7. Write a python program using for loop and range( ) function to print the
numbers 1 to 20 in reverse order.
8. Write a python program using for loop and range( ) function to print the numbers 10 to
20 in reverse order
Challenging questions
1. Write a program that uses a for loop and the range() function to print the first 10 powers of 2
(i.e., 2^0, 2^1, 2^2, ...).
2. Write a program that uses a for loop and the range() function to calculate the sum of all the
numbers from 1 to 100.
3. Write a program that uses a for loop and the range() function to calculate the sum of all the odd
numbers between 1 and 50.