Updated On : May-03,2020 Time Investment : ~10 mins

Python Programing For Beginners - Simple Python Programs Using List

What You Would Be Learning In This Tutorial

  • Program to Find the Largest Number in a List
  • Program to Merge Two Lists and Sort it
  • Program to Find the Second Largest Number in a List
  • Program To Get A Sublist In Python Using Slicing
  • Program to Check if a Number is Odd or Even
  • Program to Check Prime Number

Program to Find the Largest Number in a List

a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
    b=int(input("Enter element:"))
    a.append(b)
a.sort()
print("Largest element is:",a[n-1])
Enter number of elements:2
Enter element:3
Enter element:4
Largest element is: 4

Understanding The Program

  1. The user is asked to enter the value of the element and store it in the variable.
  2. Then the user will be asked to enter the elements of the list one by one using a for loop and store it in a list.
  3. Make sure the list should then be sorted.
  4. Then the last element of the list is printed which is also the largest element of the list.

Program to Merge Two Lists and Sort it

a=[]
c=[]
n1=int(input("Enter number of elements:"))
for i in range(1,n1+1):
    b=int(input("Enter element:"))
    a.append(b)
n2=int(input("Enter number of elements:"))
for i in range(1,n2+1):
    d=int(input("Enter element:"))
    c.append(d)
new=a+c
new.sort()
print("Sorted list is:",new)
Enter number of elements:5
Enter element:55
Enter element:67
Enter element:88
Enter element:33
Enter element:22
Enter number of elements:3
Enter element:34
Enter element:56
Enter element:99
Sorted list is: [22, 33, 34, 55, 56, 67, 88, 99]

Program to Find the Second Largest Number in a List

a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
    b=int(input("Enter element:"))
    a.append(b)
a.sort()
print("Second largest element is:",a[n-2])
Enter number of elements:4
Enter element:33
Enter element:6
Enter element:2
Enter element:4
Second largest element is: 6

Understanding The Program

  1. In this program, the user will be asked to enter the number of elements and store it in a variable.
  2. User must then enter the elements of the list one by one using a for loop and store it in a list.
  3. Again you must makes sure that the List should then be sorted.
  4. Then the last element of the list is printed which is also the largest element of the list.

Program To Get A Sublist In Python Using Slicing

# list of numbers
n_list = [1, 2, 3, 4, 5, 6, 7]

# list items from 2nd to 3rd
print(n_list[1:3])

# list items from beginning to 3rd
print(n_list[:3])

# list items from 4th to end of list
print(n_list[3:])

# Whole list
print(n_list[:])
[2, 3]
[1, 2, 3]
[4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]

Understanding The Program

Here, in the above program you can see that from a larger list we have created sublist by providing the elements. When you enter the starting value and ending value, it will display the values that come in that range.

# list of numbers
n_list = [1, 2, 3, 4]

# Changing the value of 3rd item
n_list[2] = 100

# list: [1, 2, 100, 4]
print(n_list)

# Changing the values of 2nd to fourth items
n_list[1:4] = [11, 22, 33]

# list: [1, 11, 22, 33]
print(n_list)
[1, 2, 100, 4]
[1, 11, 22, 33]

Program to Check if a Number is Odd or Even

num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("{0} is Even number".format(num))
else:
   print("{0} is Odd number".format(num))
Enter a number: 23
23 is Odd number

Program to Check Prime Number

num = int(input("Enter a number: "))

if num > 1:
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")

else:
   print(num,"is not a prime number")
Enter a number: 33
33 is not a prime number
3 times 11 is 33

Understanding The Program

A prime number is a natural number greater than 1 and having no positive divisor other than 1 and itself.

The user is asked to enter the value and the program will check whether the number is prime or not.

Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.