Most Important Questions on Python

  • By Deepali Shinkar
  • June 8, 2024
  • Python
Most Important Questions on Python

Most Important Questions on Python

Prepare for your Python interview with our guide on the Most Important Questions on Python. Master key concepts and enhance your programming skills!

 

Q. Why Python is called an interpreted language? 

An interpreted language is a language in which the implementations execute instructions directly without earlier compiling a program into machine language. 

Interpreted in simple terms means running code line by line.  

Python is both a compiled as well as an interpreted language. This means when we run a Python code, it is first compiled and then interpreted line by line. The compilation part is mostly hidden from the user. While running the code, Python generates a byte code internally, this  byte code is then converted using a Python virtual machine (p.v.m) to generate the output. 

 

Q. What is an interpreter in Python? 

A Python interpreter is a computer program that converts each high-level program statement into machine code. An interpreter translates the command that you write out into code that the computer can understand. 

 

Q. Why Python is a dynamically typed language? 

Python is dynamically typed, which means that variable types are determined and checked at runtime rather than during compilation. In dynamically typed languages like Python, you don’t need to explicitly declare the variable type before using it. 

 

Q. How can verify the object of the data type in Python? 

Python doesn’t require data types to be defined explicitly. Python provides type() and  isinstance() functions to check the type of variables. 

type() is used to verify if an object is of a certain data type. 

Syntax: type(object) 

The isinstance() function returns True if the specified object is of the specified type,  otherwise False. 

Syntax: isinstance(object,type) 

 

Q. What are built-in data types in Python? 

Built-in Data Types 

In programming, data type is an important concept. 

Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:

Text Type:  str
Numeric Types:  int, float, complex
Sequence Types:  list, tuple, range
Mapping Type:  dict
Set Types:  set, frozenset
Boolean Type:  bool
None Type:  NoneType

 

 

Q. What are literals in Python? 

A constant value that is assigned to the variable can be called a literal.

Most Important Questions on Python

 

Python Numeric Literals 

Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different  

numerical types: Integer, Float, and Complex. 

 

Integer Literals 

Integer literals are numbers without decimal parts. It also consists of negative numbers. For  

example:5,11,0,-12 etc. 

 

Floating-Point Literals 

Floating-point literals are numbers that contain decimal parts. 

Just like integers, floating-point numbers can also be both positive and negative. For example,2.5,6.76, etc. 

Complex Literals 

Complex literals are numbers that represent complex numbers. 

Here, numerals are in the form a+bj where a is real and b is imaginary. For example,6+9j. String Literals

In Python, texts wrapped inside quotation marks are called string literals.

“This is a string.”

 

We can also use single quotes to create strings. 

‘This is also a string.’

 

Boolean Literals 

There are two boolean literals: True and False. 

Character Literals 

Character literals are unicode characters enclosed in a quote. For example, 

character = ‘S’

 

Here,s is a character literal assigned to the character 

Special Literal  

Python contains one special literal None. We use it to specify a null variable. For example, value = None 

print(value) 

# Output: None 

Collection Literals 

Let’s see examples of four different collection literals. List, Tuple, Dict, and Set literals. 

collection Literals 

Let’s see examples of four different collection literals. List, Tuple, Dict, and Set literals. 

# list literal 

fruits = [“apple”, “mango”, “orange”] 

print(fruits)

# tuple literal 

numbers = (1, 2, 3) 

print(numbers) 

# dictionary literal 

alphabets = {‘a’:‘apple’, ‘b’:‘ball’, ‘c’:‘cat’} 

print(alphabets) 

# set literal 

vowels = {‘a’, ‘e’, ‘i’ , ‘o’, ‘u’} 

print(vowels) 

Run Code 

Output 

[‘apple’, ‘mango’, ‘orange’] 

(1, 2, 3) 

{‘a’: ‘apple’, ‘b’: ‘ball’, ‘c’: ‘cat’} 

{‘e’, ‘a’, ‘o’, ‘i’, ‘u’}

 

 

Q. What is coercing? 

In programming, coercing means type conversion. Type conversion is the process of converting data of one type to another. 

Two types of conversion: 

  1. Implicit Type Conversion is automatically performed by the Python interpreter.
  2. Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user.

 

Q. What does eval() do in Python? 

The Python eval() is a built-in function that allows us to evaluate the Python expression as a  ‘string’ and return the value as an integer. Programmers can use the Python eval() function to  dynamically evaluate expressions passed as a string argument to the built-in function.  

When a string is passed to Python eval(), it is parsed, compiled to bytecode, and then evaluated as a Python expression. The Python eval() function is typically employed in situations or applications that require the evaluation of mathematical expressions.

The syntax for eval() is: 

eval(expression) 

 

Q. What is an f-string? 

F-string allows you to format selected parts of a string. 

To specify a string as an f-string, simply put an f or F in front of the string literal. 

To format values in an f-string, add placeholders {}, a placeholder can contain variables,  operations, functions, and modifiers to format the value. 

A placeholder can also include a modifier to format the value. 

A modifier is included by adding a colon: followed by a legal formatting type, like .2f which means a fixed point number with 2 decimals. 

 

Q. Explain format() in Python. 

The format() method formats the specified value(s) and inserts them inside the string’s placeholder. 

The placeholder is defined using curly brackets: {}.  

The format() method returns the formatted string. 

Syntax 

string.format(value1, value2…) 

The placeholders can be identified using named variables {price}, numbered indexes {0}, or even empty placeholders {}. 

 

Q. Explain the list in Python. 

Lists are used to store multiple items in a single variable. 

Lists are created using square brackets. 

We can use list() which is a built in function and is used to create a new list. List items are ordered, changeable, and allow duplicate values. 

The list is mutable(changeable), meaning that we can change, add, and remove items in a list after it has been created.

To determine how many items a list has, use the len() function. 

List items can be of any same data type or different data type. 

 

Q. What is the difference between append() and extend() in Python? 

In list data type, append and extend functions in Python are used to add elements at the end of the list. 

append() adds a single element.  

Syntax: 

list_name.append(element) 

extend() add add multiple elements (from an iterable). 

list_name.extend(iterable) 

Accept an iterable (like a list, tuple, or string) as an argument. 

 

Q. How to Select an Element from a List? 

You access lists, and, by extension, all other sequence types, is by using the index operator [ ].  Inside, you put an integer value. 

You use the slice notation when you want to select more than one list element at a time. Syntax:list_object[start:stop:step] 

list_object[:] =>return whole list 

list_object[start:end]=> item start through the end 

list_object[start:] =>items start through the rest of the array 

list_object[:end]=>Items from the beginning through the end. 

List_object[::-1]=>Items from end to start. 

 

Q. How to create random numbers in a List?

You can create random numbers in a list with the random package. 

import random 

random.sample(range(20),k=10) 

Output: 

[16, 1, 0, 11, 19, 3, 7, 5, 10, 13] 

 

Q.How to convert list into a string? 

Most Important Questions on Python

 

 

Q. How to convert the list into a dictionary? 

Most Important Questions on Python

 

 

Q. How to Concatenate Lists in Python 

To concatenate lists, you use the + operator. It will give you a new list that is the concatenation of your two lists without modifying the original ones. 

 

Q. How to Sort a List in Python 

There are two very simple ways to get the values in your lists sorted in ascending or descending  order: 

  • You use the sort() method 
  • Or you use the sorted() function and pass a list to it

 

Q. How to Clone or Copy a List in Python 

There are a lot of ways of cloning or copying a list:

  • You can slice your original list and store it into a new variable: newList = oldList[:] or newList=oldList() 
  • You can use the built-in list() function: newList = list(oldList) 
  • You can use the copy library:
  • With the copy() method: newList = copy.copy(oldList) 
  • If your list contains objects and you want to copy those as well, you can use copy.deepcopy(): copy.deepcopy(oldList)

 

Q. Write a Python Program to Split a List Into Evenly Sized Chunks. 

Method 1: 

Using yield (generator) 

Most Important Questions on Python

 

Method 2: 

Using numpy library 

Most Important Questions on Python

 

 

Q. How to Create Flat Lists out of Lists? 

Method 1: List Comprehension

Most Important Questions on Python

 

Method 2: Using nested for loop 

Most Important Questions on Python

 

Method 3: Using itertools package 

Most Important Questions on Python

 

Method 4: Using sum function

Most Important Questions on Python

 

Method 5: Using lambda and reduce function 

Most Important Questions on Python

 

 

Q. How to Get an Intersection of Two Python Lists 

List intersection means we need to take all those elements which are common to both  of the initial lists and store them in another list.

Method 1: Using list comprehension 

Q. How to Get an Intersection of Two Python Lists

 

Method 2:Using set()

Q. How to Get an Intersection of Two Python Lists

 

Method 3:Using filter() 

Q. How to Get an Intersection of Two Python Lists

 

 

Q. Differences between lists, tuples, sets, and dictionaries. 

Q. Differences between lists, tuples, sets, and dictionaries.

 

 

Q.Write a program to reverse tuple elements.

Q.Write a program to reverse tuple elements.

 

 

Q. The given tuple is a nested tuple. write a Python program to print the value 20.

Nested tuple

 

 

Q. Write a program to unpack the following tuple into four variables and display each variable. 

four Tuples

 

 

Q. Swap two tuples in Python . 

 

 

For Free, Demo classes Call: 02071171500

Registration Link: Online Python Training in Pune!

 

Q. Given is a nested tuple. Write a program to modify the first item (22) of a list inside the following tuple to 222.

Q. Given is a nested tuple. Write a program to modify the first item (22) of a list inside the following tuple to 222.

 

 

Q. How to convert a tuple into a dictionary. 

Q. How to convert a tuple into a dictionary. 

 

 

Q. Write a Python program to calculate the average value of the numbers in a given tuple of tuples. 

Q. Write a Python program to calculate the average value of the numbers in a given tuple of tuples. 

 

 

Q. Write a Python program to convert a given tuple of positive integers into an integer.

Q. Write a Python program to convert a given tuple of positive integers into an integer.

 

 

Q. Write a Python program to compute the element-wise sum of given tuples.

sum

 

 

Q.Write a Python program to compute the sum of all the elements of each tuple stored inside a list of tuples. 

Q.Write a Python program to compute the sum of all the elements of each tuple stored inside a list of tuples. 

 

 

Q. Write a Python program to convert a given list of tuples to a list of lists.

Q. Write a Python program to convert a given list of tuples to a list of lists.

 

Do visit our channel: Click Here

 

Author:-
Deepali Shinkar
Call the Trainer and Book your free demo Class For Python

Call now!!! | SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

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

*
*