There are many methods to store numbers in a python list. Lets explore top 3 ways to store numbers in a python list.
Top 3 ways to store numbers in a python list. To understand this, we will write a python code to store contact number of 3 students.
Python code to store contact number of 3 students in a list.
Example 1:
# first create an empty list to store contact numbers
mobile_numbers = []
# add mobile numbers of 3 students to the list
mobile_numbers.append("2345678901")mobile_numbers.append("3456789012")mobile_numbers.append("4567890123")
# print the list to verify
print(mobile_numbers)
Description of the example 1:
This code will create an empty list (name of list: mobile_numbers) and then adds 3 mobile numbers to it. You can replace the mobile numbers with the actual mobile numbers of the students. Using append() function, add elements to the list. The print() function is used to verify the list.
Example 2
mobile_numbers = ['2345678901', '3456789012', '4567890123']
print(mobile_numbers)
Example 3
# Create an empty list to store contact numbersstudent_numbers = []# Read contact numbers and append it to a listfor i in range(5):number = input(f"Enter contact number of student{i+1}: ")student_numbers.append(number)# Print list of contact numbersprint("Contact numbers of 5 students:")print(student_numbers)
* * * * *
No comments:
Post a Comment