Updated On : May-06,2020  python, basics

# Python Programs For Beginners - Simple & Interesting¶

### Simple Python Program To Make The Learning Process Simple And Interesting¶

• Program to Check whether Year is a Leap Year or not
• Program to Check Vowel or Consonant
• Program To Add Two Binary Numbers
• Program To Add Two Numbers
• Program to Check If a number is Prime or not
• Program to Check If number is Even or Odd
• Program to Find Factorial of Number
• Program to Calculate length of a String

### Program to Check whether Year is a Leap Year or not¶

In [1]:
```# User enters the year
year = int(input("Enter Year: "))

# Leap Year Check
if year % 4 == 0 and year % 100 != 0:
print(year, "is a Leap Year")
elif year % 100 == 0:
print(year, "is not a Leap Year")
elif year % 400 ==0:
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")
```
```Enter Year: 2020
2020 is a Leap Year
```

### Program to Check Alphabet¶

In this program, user is asked to enter a character and the input character is stored in a variable. The program checks whether the entered character lies in the range of lowercase or uppercase alphabets, if it does then the program displays the message that the “character is an Alphabet” else it displays that the “character is not an Alphabet”.

In [4]:
```# taking user input
ch = input("Enter a character: ")
if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')):
print(ch, "is an Alphabet")
else:
print(ch, "is not an Alphabet")
```
```Enter a character: 2
2 is not an Alphabet
```

### Program to Check Vowel or Consonant¶

In [3]:
```# taking user input
ch = input("Enter a character: ")

if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I'
or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u'):
print(ch, "is a Vowel")
else:
print(ch, "is a Consonant")
```
```Enter a character: a
a is a Vowel
```

### Program To Add Two Binary Numbers¶

In [5]:
```# decimal value 1
num1 = '00001'
# decimal value 17
num2 = '10001'

# sum - decimal value 18
# binary value 10010
sum = bin(int(num1,2) + int(num2,2))
print(sum)
```
```0b10010
```

### Program To Add Two Numbers¶

You can even ask the user to input the values. For that you would have to make use of input() build-in function.

Method:1

In [7]:
```# two float values
val1 = 100.99
val2 = 76.15

# Adding the two given numbers
sum = float(val1) + float(val2)

print("The sum of given numbers is: ", sum)
```
```The sum of given numbers is:  177.14
```

Method 2:

In [8]:
```# Getting the values of two numbers from user
val1 = float(input("Enter first number: "))
val2 = float(input("Enter second number: "))

sum = val1 + val2

# Displaying the result
print("The sum of input numbers is: ", sum)
```
```Enter first number: 23
Enter second number: 44
The sum of input numbers is:  67.0
```

### Program to Check If a number is Prime or not¶

In [9]:
```# taking input from user
number = int(input("Enter any number: "))

# prime number is always greater than 1
if number > 1:
for i in range(2, number):
if (number % i) == 0:
print(number, "is not a prime number")
break
else:
print(number, "is a prime number")

# if the entered number is less than or equal to 1
# then it is not prime number
else:
print(number, "is not a prime number")
```
```Enter any number: 33
33 is not a prime number
```

### Program to Check If number is Even or Odd¶

In [10]:
```num = int(input("Enter any number: "))
flag = num%2
if flag == 0:
print(num, "is an even number")
elif flag == 1:
print(num, "is an odd number")
else:
print("Error, Invalid input")
```
```Enter any number: 55
55 is an odd number
```

### Program to Find Factorial of Number¶

Concept Of Factorial

Factorial of a number is the product of an integer and all the integers below it, for example the factorial of 4 is 432*1 = 24. Her we will be making use of the python function factorial(). The function will calculate the factorial abd then display.

In [12]:
```def factorial(num):
"""This is a recursive function that calls
itself to find the factorial of given number"""
if num == 1:
return num
else:
return num * factorial(num - 1)

# We will find the factorial of this number
num = int(input("Enter a Number: "))

# if input number is negative then return an error message
# elif the input number is 0 then display 1 as output
# else calculate the factorial by calling the user defined function
if num < 0:
print("Factorial cannot be found for negative numbers")
elif num == 0:
print("Factorial of 0 is 1")
else:
print("Factorial of", num, "is: ", factorial(num))
```
```Enter a Number: 5
Factorial of 5 is:  120
```

### Program to Calculate length of a String¶

In [11]:
```# User inputs the string and it gets stored in variable str
str = input("Enter a string: ")

# counter variable to count the character in a string
counter = 0
for s in str:
counter = counter+1
print("Length of the input string is:", counter)
```
```Enter a string: dolly is good girl.
Length of the input string is: 19
```
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