Updated On : May-03,2020  python, basics

# 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¶

In [1]:
```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¶

In [3]:
```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¶

In [4]:
```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¶

In [5]:
```# 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.

In [7]:
```# 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¶

In [8]:
```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¶

In [9]:
```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

## Support Us

Thank You for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the Coffee button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.

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

If you want to

• provide some suggestions on topic