Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Shot Interview

Uploaded by

enet5566
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Shot Interview

Uploaded by

enet5566
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Here’s a simple explanation of each question you asked, broken down into easy terms:

Basic Concepts:

1. What does it mean that Python is an interpreted language?

o Python code is executed line by line by an interpreter, meaning Python doesn’t need
to be compiled before running like other languages (e.g., C or Java).

2. What is indentation in Python? Why is it important?

o Indentation means leaving spaces at the beginning of a line of code. In Python, this is
important because it tells Python which lines of code belong to which blocks, like
loops or functions. Without proper indentation, Python won’t understand your code.

3. What are the data types in Python?

o Data types are different kinds of values in Python. Some common data types are:

▪ int (for numbers)

▪ float (for decimal numbers)

▪ str (for text)

▪ list (for ordered collections of items)

▪ dict (for key-value pairs)

4. Why do we need different data types in Python?

o We need different data types because different types of data behave differently. For
example, numbers can be added or multiplied, but strings (text) are used for writing
words or sentences.

5. How do we check the data type of a variable in Python?

o You can use the type() function to check the data type of a variable. Example: type(5)
will return int because 5 is a number.

Python Data Structures:

6. What are Python sets, and what are their properties?

o A set is a collection of unique items. It does not allow duplicates, and the items are
unordered, meaning the order doesn’t matter.

7. What’s the difference between a list and a tuple in Python?

o A list is mutable (can be changed) and ordered, while a tuple is immutable (cannot
be changed) and also ordered. Lists use square brackets [], and tuples use
parentheses ().

8. What does mutable and immutable mean in Python?


o Mutable means that something can be changed after it’s created (like a list).
Immutable means it cannot be changed (like a tuple or a string).

9. What is a dictionary in Python?

o A dictionary stores data in key-value pairs. For example, {"name": "Alice", "age": 25}
is a dictionary where "name" is the key, and "Alice" is the value.

10. How can you access an element of a list in Python?

o You can access elements by their index. For example, my_list[0] will give you the first
element in my_list.

Type Conversion and Operations:

11. How do you convert a string to an integer in Python?

o You can use the int() function to convert a string to an integer. Example: int("5") will
give you the number 5.

12. How do you convert a string to a list in Python?

o You can use the list() function or split() method. Example: "hello".split() will give ['h',
'e', 'l', 'l', 'o'].

13. What are comparison operators in Python?

o These are used to compare values. For example:

▪ == checks if two values are equal.

▪ != checks if two values are not equal.

▪ > checks if one value is greater than another.

Python Functions:

14. What are the benefits of using functions in Python?

o Functions help you organize and reuse code. Instead of writing the same code
multiple times, you can write a function and use it whenever needed.

15. How do Python functions help with code optimization?

o Functions make your code cleaner, easier to manage, and prevent duplication, which
makes your program run more efficiently.

Indexing and Slicing:

16. What is indexing and negative indexing in Python?

o Indexing refers to accessing an element in a list or string using its position. Example:
my_list[0] accesses the first element.
o Negative indexing lets you count from the end. Example: my_list[-1] gives you the
last element.

17. What is slicing in Python?

o Slicing allows you to extract parts of a list or string. Example: my_list[1:4] gives you a
new list with elements from index 1 to 3.

Operators:

18. What are logical operators in Python?

o Logical operators are used to combine conditions:

▪ and: returns True if both conditions are true.

▪ or: returns True if at least one condition is true.

▪ not: reverses the result, returning True if the condition is false.

Object-Oriented Programming:

19. What are classes and objects in Python?

o A class is like a blueprint for creating objects. An object is an instance of a class.


Example: If Car is a class, then a specific car like "Honda" is an object of that class.

20. How can you check if a year is a leap year in Python?

o You check if the year is divisible by 4 but not by 100, unless it’s also divisible by 400.
You can use an if statement to do this.

21. How can you check if a number is an Armstrong number in Python?

o An Armstrong number is a number that is equal to the sum of its digits raised to the
power of the number of digits. Example: 153 is an Armstrong number because
13+53+33=1531^3 + 5^3 + 3^3 = 15313+53+33=153.

22. How can you check if a string is a palindrome in Python?

o A palindrome reads the same forward and backward. You can check if string ==
string[::-1].

23. How can you reverse a string in Python?

o You can reverse a string using slicing: string[::-1].

24. How do you swap two variables in Python?

o You can swap two variables like this: a, b = b, a.

25. How can you check if a number is prime in Python?

o A prime number is only divisible by 1 and itself. You can use a loop to check if it has
any divisors other than 1 and itself.
Math Functions:

26. How can you calculate the factorial of a number in Python?

o The factorial of a number is the product of all numbers from 1 to that number. You
can calculate it using a loop or the math.factorial() function.

27. How can you generate random numbers in Python?

o You can use the random module to generate random numbers. Example:
random.randint(1, 10) generates a random integer between 1 and 10.

28. How can you print the Fibonacci series in Python?

o The Fibonacci series starts with 0 and 1, and each next number is the sum of the
previous two numbers. You can print it using a loop or recursion.

29. How do you find the square root of a number in Python?

o You can use math.sqrt() to find the square root of a number.

30. How do you swap two elements in a list in Python?

o You can swap two elements like this: list[index1], list[index2] = list[index2],
list[index1].

Python Syntax:

31. Why is indentation so important in Python?

o Indentation tells Python where blocks of code start and end. If indentation is wrong,
Python won’t understand the code.

Modules and Packages:

32. What is the difference between a module and a package in Python?

o A module is a single Python file with functions and classes. A package is a collection
of modules in a folder.

Advanced Python:

33. What are decorators in Python, and how are they used?

o A decorator is a function that modifies the behavior of another function. It’s used to
add functionality without changing the original code.

34. What is exception handling in Python?

o Exception handling helps you deal with errors in your program. You use try and
except blocks to catch and handle errors.
35. What is inheritance in Python?

o Inheritance allows a class to inherit properties and methods from another class. It
helps you reuse code.

36. What’s the difference between is and == in Python?

o == checks if two values are equal. is checks if two variables point to the same object
in memory.

37. What are generators in Python, and why are they useful?

o Generators are functions that return values one at a time using the yield keyword.
They are useful for handling large data sets because they don’t store the entire result
in memory.

38. What is the difference between shallow copy and deep copy in Python?

o A shallow copy copies only the references to objects, while a deep copy creates a
new copy of the objects.

Python Expressions:

39. What are lambda expressions in Python?

o Lambda expressions are small, anonymous functions written in one line. Example:
lambda x: x * 2.

40. What is the pass keyword used for in Python?

o pass is a placeholder when you don’t want to write any code in a block but want to
avoid an error.

Common Tasks:

41. How do you reverse a string in Python?

o You can reverse a string with string[::-1].

42. How do you sort a list in Python?

o Use the sort() method to sort a list in ascending order.

43. How do you delete a file in Python?

o You can use the os.remove() function to delete a file.

44. How do you delete an element from a list in Python?

o Use the remove() method to delete a specific element from a list.

45. How do you delete an entire list in Python?

o Use the del statement to delete a list: del my_list.


46. How do you reverse an array in Python?

o You can reverse an array using slicing: array[::-1].

Numpy:

47. How do you access, delete, and update elements in a NumPy array?

o You access elements with array[index], delete with np.delete(), and update by
assigning new values to specific indices.

List Operations:

48. How do you concatenate (combine) two lists in Python?

o You can combine two lists with the + operator. Example: list1 + list2.

49. How do you calculate the square of all elements in a list in Python?

o You can use a list comprehension: [x**2 for x in my_list].

String Operations:

50. What is the difference between split and join in Python?

o split() breaks a string into a list, while join() combines a list of strings into a single
string.

You might also like