Complete 50 Basic Python Questions
Complete 50 Basic Python Questions
1. What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability. It's
widely used in web development, data analysis, automation, and machine learning.
Python is easy to learn, supports object-oriented and functional programming, has a large standard
Common data types include int, float, str, bool, list, tuple, dict, and set.
Lists are mutable (can be changed), while tuples are immutable (cannot be changed).
A dictionary stores key-value pairs. Its unordered and mutable. Example: {'name': 'Manoj', 'age': 22}
Variables are used to store data. Python variables are dynamically typed, meaning their type is
inferred.
Type casting converts one data type to another. Example: int('5') converts string to integer.
Indentation defines blocks of code. It replaces curly braces used in other languages. Incorrect
9. What is a function?
A function is a reusable block of code. Defined using 'def'. Example: def greet(): print('Hello')
return sends a result back to the caller, while print displays it on the screen.
They allow branching logic: if, elif, else. Example: if x > 0: print('Positive')
Loops repeat code. for and while are the two types. 'break' exits the loop, 'continue' skips to the next
iteration.
A file containing Python code (functions, variables). Use import to include it.
*args allows variable number of positional arguments. **kwargs allows variable number of keyword
arguments.
24. What is the difference between local and global variables?
Local variables are defined inside functions. Global variables are accessible throughout the
program.
A function that calls itself. Must have a base case to stop recursion.
Functions that yield values one by one using 'yield'. Efficient for large data.
A function that modifies another function. Used for logging, access control, etc.
35. What are Python files and how do you read them?
Use open('file.txt', 'w') and write(). Always close files or use with statement.
37. What is object-oriented programming (OOP)?
OOP involves classes and objects. Python supports inheritance, polymorphism, encapsulation.
Represents the instance of a class. Used to access variables that belong to the class.
One class can inherit features from another class. class B(A):
Class variables are shared. Instance variables are unique to each object.
48. What is the difference between shallow copy and deep copy?