10-Python-Coding-Questions-To-Practice-and-Enhance-Logical-Thinking

This article aims to provide you with an approachable collection of the most commonly asked Python basic programming questions. These questions require no prior programming knowledge and will help you identify what areas need more practice.

Introduced in 1991, Python has emerged as one of the world’s most famous high-level programming languages. Naturally, it has become a go-to weapon of choice for many new and professional programmers due to a syntax that’s easy to read, learn and write.

However, having a good understanding of mathematics or an accessible syntax can only get you so far if the logic behind the solution is flawed. The difference between a mediocre programmer and a great one is the ability to think logically and apply reasoning to find the most efficient solution to a given problem, no matter the coding language.

Thankfully, like everything else in programming, applying logic while coding is a learnable skill where practice makes perfect.

Let us look at some complex yet rewarding Python basic programming questions to flex those logic muscles in your mind.

10-Python-Coding-Questions-To-Practice-and-Enhance-Logical-Thinking

10 Python Basic Programming Questions to Practice

Python program to check given number is even or odd

As all even numbers are perfectly divisible by two and leave ‘zero’ as a reminder, those that don’t can be classified as odd. Our program simply divides the number by ‘2’ and checks the remainder to classify the input number.

#taking input from the user
num = int(input(“Enter a number to check even/odd “))
#if number is divisible by 2
if num%2 == 0:
  print(num,”is even number”)
else:
  print(num,”is odd number”)

Python basic programming questions to calculate the square of a number

This one simply multiples the number by itself to give us the square of the input.

Also Read:  The Beginner’s Guide to Becoming a Full-Stack Java Developer
#take input from the user
num = int(input(“Enter a number to calculate square : “))
print(“square =”,num*num)

Python program to calculate the cube of a number

Here, the number is multiplied by itself twice to get the cube of the input as the output.

#take input from the user
num = int(input(“Enter a number to calculate cube : “))
print(“cube =”,num*num*num)

Python program to check given character is a vowel or consonant

This python program will use an array that stores all vowels and then deploy an if-else loop condition to determine whether the input character is a vowel or a consonant.

Ch=input(“Enter a character to check vowel or consonant :”)
if(ch == ‘a’ or ch == ‘e’ or ch == ‘i’ or ch == ‘o’ or ch == ‘u’ or ch == ‘A’ or ch == ‘E’ or ch == ‘I’ or ch == ‘O’ or ch == ‘U’):
#if ‘if’ condition satisfy
print(“Given character”, ch ,”is vowel”)
else:
#if ‘if’ condition does not satisfy the condition
print(“Given character”,ch, “is  consonant”)

Python program to count vowels and consonants in the string

This program employs the use of variables to store the occurrence of vowels and consonants in a string by looping through the entire string.

#taking input from the user
string = input(“Enter a String : “)
vowels = 0  #variable to count number of vowels
consonants = 0 #variable to count number of consonants
for i in string:  #string iteration
  if i in (‘a’, ‘e’, ‘i’, ‘o’, ‘u’,’A’, ‘E’, ‘I’, ‘O’, ‘U’):  #if character in string is vowel
      vowels+=1 #if vowel increment variable ‘vowel’ with one
  elif i.isalpha():  #checking if the character is alphabet
      consonants+=1  #if consonant increment variable ‘consonants’ with one
print(“Vowels :”,vowels,”Consonants:”,consonants)
Also Read:  What Do Employers Value More-PG Diploma in Software Development or a degree.

Python program to remove spaces from string without inbuilt function

This program achieves string concatenation and removes all spaces from a given string without using the Python’s inbuilt function to achieve the same.

#taking input from the user
string = input(“Enter a String : “)
result=”
#iterating the string
for i in string:
  #if the character is not a space
  if i!=’ ‘:
      result += i
print(“String after removing the spaces :”,result)

Python program to convert Fahrenheit into Celsius

This program uses the formula( ( Fahrenheit – 32)*5 ) / 9 ) to convert input temperature from Fahrenheit to Celsius.

#taking input from the user
fahrenheit = float(input(“Please give the Fahrenheit Temperature : “))
#converting Celsius into Fahrenheit
celsius = ((fahrenheit-32)*5)/9
print(“Celcius= “,celsius)

Python program to find smallest number among three

This program compares three input numbers to find the smallest.

a = int(input(“Enter the value for a :”))
b = int(input(“Enter the value for b :”))
c = int(input(“Enter the value for c :”))
#comparing integer ‘a’ with other two integer
if a<=b and a<=c:
print(“a is smallest”)
#comparing integer ‘b’ with other two integer
elif b<=a and b<=c:
print(“b is smallest”)
#comparing integer ‘c’ with other two integer
elif c<=a and c<=b:
print(“c is smallest”)

Python program to calculate Factorial using recursion 

This program calculates the factorial of the input number using recursion including explanation.

def fact(n):
  if n == 1:
      return n
  else:
#recursion
      return n*fact(n-1)
num = int(input(“Enter a whole number to find Factorial: “))
if num < 0:
  print(“Factorial can’t be calculated for negative number”)
elif num == 0:
  print(“Factorial of 0 is 1”)
else:
  print(“Factorial of”,num,”is”,fact(num))
Also Read:  10 Basic C++ Questions to Ease Your Interview Prep

Python program to find L.C.M. of two numbers 

This program calculates the L.C.M. of two given numbers by employing comparison and calculation.

num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
if num1 > num2:
  greater = num1
else:
  greater = num2
while(True):
  if((greater % num1 == 0) and (greater % num2 == 0)):
      lcm = greater
      break
  greater += 1
print(“LCM of”,num1,”and”,num2,”=”,greater)

These were a few helpful questions to enhance your logical thinking while coding and help you write more efficient code which solves complex problems logically.

Practising these questions is a sure-shot way to enhance your logical thinking while writing code in any language, as this skill is transferable to almost all fields of programming.

If you want to learn more, you can join paid internship program that focuses on project-based learning and becomes a skilled, logical, full-stack developer. Check out CodeQuotient.


Get UGC Approved, BCA Degree
And Earn While You Do That

Join CodeQuotient's Under-Graduate Program In Software Engineering

Get Paid Internship with Our Hiring Partners to Sponsor Your Fees

100% Placement Assistance


Leave a Reply

Your email address will not be published. Required fields are marked *

Archives

About  |  Blog  |  Contact  |  Privacy Policy  |  Terms and Conditions