Basic Python Coding Questions and Answers

article-featured-image

Python is one of the most powerfull programming language. Python is used in web-development, machine learning, data science, data scraping and the list is endless. This article is about Basic-level Questions and their answers. If you are starting your Python journey, then this article is for you.

Question: 1

Write a program that takes in two numbers from the user and prints their sum.

num_1 = int(input("Enter first number: "))
num_2 = int(input("Enter second number: "))

answer = num_1 + num_2
print(f"Sum of {num_1} and {num_2} is: {answer}")

# output
Enter first number: 5
Enter second number: 6
Sum of 5 and 6 is: 11
Question: 2

Write a program that takes in a string from the user and prints out each character of the string on a new line.

user_input = input("Enter any string: ")

for i in user_input:
    print(i)

# output
Enter any string: hello
h e l l o
Question: 3

Write a program that takes in a list of numbers from the user and prints the largest and smallest number in the list.

list = [1,2,3,4,5,6,7,8,9,10]

print(f"Largest number is: {max(number_list)}")
print(f"Smallest number is: {min(number_list)}")

# output
Largest number is: 10
Smallest number is: 1
Question: 4

Write a program that takes in a list of words from the user and prints out the length of each word on a new line.

word_list = ['hello', 'world']

for i in word_list:
    print(f"Length of '{i}' is {len(i)}")

# output
Length of 'hello' is 5
Length of 'world' is 5
Question: 5

Write a program that takes in a list of numbers from the user and prints new list that should consist of even numbers only.

number_list = [1,2,3,4,5,6,7,8,9]
even_number_list = []

for i in number_list:
    chk = i % 2
    if chk == 0:
        even_number_list.append(i)

print(even_number_list)

# output
Even numbers in the list are: [2, 4, 6, 8]
Question: 6

Write a program that takes in a string from the user and prints out whether the string is a palindrome or not.

user_input = input("Enter any word: ")

chk = user_input[::-1]
if chk == user_input:
    print("String is palindrome")
else:
    print("String is not palindrome")

# output
Enter any word: eye
String is palindrome
Question: 7

Write a program that takes list of numbers and print only even numbers in new list.

list1 = [1,2,3,4,5,6,7,8,9]
list2 = [2,4,6,8]
list3 = []

# Method 1

for i in list1:
    if i in list2:
        list3.append(i)
print(f"Method 1: {list3}")

# Method 2

list3 = list(set(list1).intersection(list2))
print(f"Method 2: {list3}")

# output
Method 1: [2, 4, 6, 8]
Method 2: [8, 2, 4, 6]
Question: 8

Write a program that takes number as user input and print if that number is even or odd.

user_input = int(input("Enter any number: "))
chk = user_input % 2

if chk == 0:
    print(f"{user_input} is even number")
else:
    print(f"{user_input} is odd number")

# output
Enter any number: 10
10 is even number
Question: 9

Write a program that takes string as user input and print reverse of that string.

user_input = input("Enter any string: ")
rev = user_input[::-1]
print(f"Reverse of {user_input} is: {rev}")

# output
Enter any string: hello world
Reverse of hello world is: dlrow olleh
Question: 10

Write a python function that takes a string and integer as arguments from user input. It should return a new string that consists of original string repeated by number of times specified as integer by user.

usr_input_1 = input("Enter any string: ")
usr_input_2 = int(input("Enter any number: "))

def str_and_int(para1, para2):
    return usr_input_1 * usr_input_2

print(str_and_int(usr_input_1, usr_input_2))

# output
Enter any string: hello
Enter any number: 5
hellohellohellohellohello
Question: 11

Write a python function takes list of integers and return sum of only even numbers.

def sum_even(para):
    even = []
    for i in para:
        if (i % 2) == 0:
            even.append(i)
    return sum(even)

print(f"Sum of even numbers is: {sum_even([1, 2, 3, 4, 5, 6, 7, 8, 9])}")

# output
Sum of even numbers is: 20
Question: 12

Write function to find largest even number from the list. If largest number is not even, print the appropriate message.

def largest_even(para):
    max_number = max(para)
    if max_number % 2 == 0:
        return "Largest number is even"
    else:
        return "Largest number is not even"

number_list = [1, 20, 3, 4, 5, 6, 7, 9]
print(largest_even(number_list))

# output
Largest number is even
Question: 13

Write a function that takes list of integers and print the sum of all numbers that are divisible by 3 or 6.

def sum_func(para):
    req_sum = 0
    for i in para:
        if (i % 3) == 0 or (i % 6) == 0:
            req_sum += i
    return req_sum

int_list = [3, 6, 9, 12, 13, 14, 17, 19, 20]
result = sum_func(int_list)
print(f"Result is: {result}")

# output
Result is: 30

These are some of the basic questions to understand the logic in Python. More questions will be added soon.

Basic Python Logic
protocolten-admin

Author: Harpreet Singh

Created: Mon 27 Mar 2023

Updated: 1 year, 2 months ago

POST CATEGORY
  1. Linux
  2. Knowledge
  3. Programming
Suggested Posts:
LINUX post image
How to setup on-premise MySQL master and slave servers

In this article I'll be demonstrating how you can configure MySQL master and slave …

LINUX post image
Configure FastAPI with Nginx and uvicorn

This article is all about the configuration of FastAPI with uvicorn and then reverse …

KNOWLEDGE post image
Read Binary Bits with these effective methods

You are most likely familiar with the concept of Binary Language. Yes, you guessed …

NETWORKING post image
CTF Challenges and Solutions from picoGYM by picoCTF

picoCTF is an open-source project. It's an enhanced platform for education and organizing competitions …

WINDOWS post image
Reset windows 10 password using bootable usb drive

Windows 10 by Microsoft is the most used operating system nowadays. Despite being heavily …

Sign up or Login to post comment.

Comments (0)