# 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
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”.
# 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
# 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
# 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)
You can even ask the user to input the values. For that you would have to make use of input() build-in function.
# two float values val1 = 100.99 val2 = 76.15 # Adding the two given numbers sum = float(val1) + float(val2) # Displaying the addition result print("The sum of given numbers is: ", sum)
The sum of given numbers is: 177.14
# Getting the values of two numbers from user val1 = float(input("Enter first number: ")) val2 = float(input("Enter second number: ")) # Adding the numbers 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
# 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
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
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.
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
# 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
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
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 firstname.lastname@example.org. 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.
If you want to