What is Python?
Python is an interpreted, high-level programming language characterized by its readability and simplicity. Its design philosophy emphasizes code readability, allowing programmers to express concepts in fewer lines of code than might be used in other languages. With its dynamic typing and dynamic binding, Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
What are Python’s key features?
Python boasts numerous key features that contribute to its popularity, including:
- Readability: The syntax is clear and intuitive, which enhances maintainability.
- Interpreted Language: Python code is executed line by line, facilitating debugging and interactive programming.
- Dynamically Typed: Variable types are determined at runtime, allowing for flexibility in coding.
- Extensive Libraries: A vast collection of libraries and frameworks, such as NumPy, Pandas, and Flask, empowers developers to tackle diverse tasks effortlessly.
- Cross-Platform: Python runs seamlessly across different operating systems, making it a versatile choice for developers.
How do you install Python?
Installing Python is a straightforward process. Download the latest version from the official Python website, select the appropriate installer for your operating system, and follow the installation instructions. For most systems, adding Python to the PATH is an essential step to ensure that it can be executed from the command line.
What are Python comments?
Comments in Python are annotations within the code that are not executed. They serve to explain or clarify the purpose of certain code sections, enhancing readability for others (or for the future self). Comments are created using the # symbol for single-line comments and triple quotes (”’ or “””) for multi-line comments.
100+ Python Interview Questions
Basic Level Coding Questions
1. Given an integer,n, perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird
n = int(input("Enter an integer: "))
if n % 2 != 0:
print("Weird")
elif 2 <= n <= 5:
print("Not Weird")
elif 6 <= n <= 20:
print("Weird")
else:
print("Not Weird")
2. Print Function problem in Python.
The included code stub will read an integer, n, from STDIN. Without using any string methods, try to print the following: 123……..n
Note that “…” represents the consecutive values in between.
n = int(input()) # Read an integer from the user
for i in range(1, n + 1):
print(i, end='') # Print numbers from 1 to n
3. Find the Runner-up Score in Python.
Given the participants’ score sheet for your University Sports Day, you must find the
runner-up score. You are given n scores. Store them in a list and find the score of the
runner-up
n = int(input()) # Read the number of elements
arr = list(map(int, input().split())) # Read the array elements
arr1 = set(arr) # Remove duplicates
arr2 = sorted(arr1) # Sort the array
print(arr2[-2]) # Print the second largest number
4. Finding the Percentage in Python?
The provided code stub is read in a dictionary containing key/value pairs of name:[Marks]
for a list of students. Print the average of the marks array for the student name is provided,
showing 2 places after the decimal.
n = int(input()) # Read the number of students
student_marks = {} # Dictionary to store student names and their marks
for _ in range(n):
name, *line = input().split()
scores = list(map(float, line))
student_marks[name] = scores
query_name = input() # Read the query student name
l1 = list(student_marks[query_name])
addition = sum(l1)
result = addition / len(l1)
print('%.2f' % result) # Print the average with two decimal places
5. To solve the Python list problem.
Consider a list (list = []). You can perform the following commands:
● insert i, e: Insert integer e at position i.
● print: Print the list.
● remove e: Delete the first occurrence of integer e.
● append e: Insert integer e at the end of the list.
● sort: Sort the list.
● pop: Pop the last element from the list.
● reverse: Reverse the list.
Initialize your list and read in the value of n followed by n lines of commands where each
command will be of the 7 types listed above. Iterate through each command in order and
perform the corresponding operation on your list.
N = int(input()) # Get the number of commands
List = [] # Initialize an empty list
for i in range(N):
command = input().split() # Split the input into a list of commands and arguments
if command[0] == "insert":
List.insert(int(command[1]), int(command[2])) # Insert an element at a specific position
elif command[0] == "append":
List.append(int(command[1])) # Add an element to the end of the list
elif command[0] == "pop":
List.pop() # Remove the last element from the list
elif command[0] == "print":
print(List) # Print the current state of the list
elif command[0] == "remove":
List.remove(int(command[1])) # Remove the first occurrence of a specific element
elif command[0] == "sort":
List.sort() # Sort the list in ascending order
else:
List.reverse() # Reverse the order of the list
6. To solve a SWAP CASE Python problem.
Task
You are given a string and your task is to swap cases. In other words, convert all
lowercase letters to uppercase letters and vice versa.
def swap_case(s):
# Initialize an empty string to store the result
string = ""
# Iterate over each character in the input string
for i in s:
# Check if the character is uppercase
if i.isupper():
string += i.lower()
else:
string += i.upper()
# Return the resulting string with swapped case
return string
if __name__ == '__main__':
# Get the input string from the user
s = input()
result = swap_case(s)
# Print the resulting string with swapped case
print(result)