Simple Python Programs That You Would Learn Through This Tutorial

- Python Program to Calculate the Average of Numbers in a Given List
- Program to Exchange the Values of Two Numbers Without Using a Temporary Variable
- Program to Reverse a Given Number
- Program to Check Whether a Number is Positive or Negative
- Program to Find the Sum of Digits in a Number
- Program to Print Odd Numbers Within a Given Range
- Program to Count the Number of Digits in a Number
- Program to Check if a Number is a Palindrome

In [1]:

```
n=int(input("Enter the number of elements to be inserted: "))
a=[]
for i in range(0,n):
element=int(input("Enter element: "))
a.append(element)
avg=sum(a)/n
print("Average of elements in the list",round(avg,2))
```

**The program will be making use of "For Loop"**

- The user is required to enter the number which will be stored in the variable 'n'.
- The value of the variable 'i' will range from 0 to the 'number of element' entered by the user.
- The number entered by the user will be stored in the variable 'element'.
- The built-in function will append the number to the list
- Now the value of i is incremented to 2.
- Every new value entered by the user will iterate and get stored in the variable "element" and then appended to the list.
- Then the 'sum' of all the values will be calculated and after that average.

In [2]:

```
a=int(input("Enter value of first variable: "))
b=int(input("Enter value of second variable: "))
a=a+b
b=a-b
a=a-b
print("a is:",a," b is:",b)
```

- In this program, the user is asked to enter the two values for both the elements.
- The first element is assigned the sum of the first two elements.
- The second element is assigned the difference between the sum in the first variable and the second variable, which is basically the first element.
- Swapping of the numbers is carried out by the difference between the sum in the variable and the second variable, which is the second element.
- Then the swapped values are printed.

In [3]:

```
n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)
```

- In this program, the user is asked to enter the value and store it in a variable n. (To witness the reversal, you must enter a two-digit number).
- The while loop is used and the last digit of the number is obtained by using the modulus operator.
- The last digit is then stored at the one’s place, second last at the ten’s place, and so on.
- The last digit is then removed by truly dividing the number with 10.
- This loop terminates when the value of the number is 0.
- In the final step, the reverse of the number is then printed.

In [4]:

```
n=int(input("Enter number: "))
if(n>0):
print("Number is positive")
else:
print("Number is negative")
```

- Users must first enter the value and store it in a variable.
- Use an if statement to make a decision.
- If the value of the number is greater than 0, “Number is positive” is printed.
- If the value of the number if lesser than 0, ”Number is negative” is negative.

In [5]:

```
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
```

- Users must first enter the value and store it in a variable.
- The while loop is used and the last digit of the number is obtained by using the modulus operator.
- The digit is added to another variable each time the loop is executed.
- This loop terminates when the value of the number is 0.
- The total sum of the number is then printed.

In [6]:

```
lower=int(input("Enter the lower limit for the range:"))
upper=int(input("Enter the upper limit for the range:"))
for i in range(lower,upper+1):
if(i%2!=0):
print(i)
```

- Users must enter the upper range limit and the lower range limit.
- The for loop ranges from the lower range limit to the upper range limit.
- The expression within the if-statement checks if the remainder obtained when the number divided by 2 is one or not (using the % operator).
- If the remainder isn’t equal to 0, the number is odd and hence the number is printed.

In [7]:

```
n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
print("The number of digits in the number are:",count)
```

- Here the user must first enter the value of the integer and store it in a variable.
- The while loop is used and the last digit of the number is obtained by using the modulus operator.
- Each time a digit is obtained, the count value is incremented.
- This loop terminates when the value of the number is 0.
- The total count of the number of digits is printed.

In [8]:

```
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
```

- Users must first enter the value of the integer and store it in a variable.
- The value of the integer is then stored in another temporary variable.
- The while loop is used and the last digit of the number is obtained by using the modulus operator.
- The last digit is then stored at the one’s place, second last at the ten’s place, and so on.
- The last digit is then removed by truly dividing the number with 10.
- This loop terminates when the value of the number is 0.
- The reverse of the number is then compared with the integer value stored in the temporary variable.
- If both are equal, the number is a palindrome.
- If both aren’t equal, the number isn’t a palindrome.
- The final result is then printed.

Dolly Solanki