Free Python Flashcards | Simplify Your Learning Journey
Arrow keys or swipe to navigate cards
Simplify your Python learning journey with our free flashcards on Stellar Study Cards. Master Python concepts easily and effectively. Get started now!
What is a variable in Python? A variable in Python is a reserved memory location to store values. It acts as a container for data and gives it a specific name.
How do you create a comment in Python?
In Python, comments are created by placing a hash (#) symbol before the text. For example:
What is the output of the expression
How do you start defining a function in Python?
You start defining a function in Python using the
What is the purpose of the
What is a variable in Python? A variable in Python is a named location used to store data in the memory. It acts as a placeholder for data that can be changed during program execution.
What are the basic data types available in Python? The basic data types in Python include integers, floating-point numbers, strings, and booleans.
How do you declare a string variable in Python?
A string variable can be declared in Python using single or double quotes. For example,
What is the use of the
How do you convert a string to an integer in Python?
To convert a string to an integer in Python, you use the
What is a floating-point number in Python? A floating-point number in Python represents a real number with a fractional part, denoted by a decimal point. For example, 3.14 is a floating-point number.
What does a 'for' loop do in Python?
A 'for' loop in Python is used to iterate over a sequence (such as a list, tuple, dictionary, set, or string) and execute a block of code multiple times. For example:
How does an 'if' statement work in Python?
An 'if' statement in Python is used to execute a block of code if a specified condition is true. It follows the structure:
What is the purpose of the 'break' statement in a loop?
The 'break' statement is used in loops to terminate the loop prematurely when a certain condition is met, and the program control moves to the code after the loop. For instance,
How can you use a 'while' loop in Python?
A 'while' loop in Python repeatedly executes a block of code as long as the specified condition is true. It has the structure:
What is the role of the 'continue' statement in a loop?
The 'continue' statement is used in a loop to skip the remaining code inside the loop for the current iteration, and proceed to the next iteration. It allows for conditional logic inside loops. For example,
Explain the use of the 'else' clause with loops.
In Python, a loop can have an 'else' clause, which executes after the loop finishes iterating over a sequence as long as no 'break' statement interrupts the loop. For example:
What is the function of a 'pass' statement in Python?
The 'pass' statement in Python is a null operation; it is used as a placeholder in a block of code where syntactically some code is required, but nothing needs to be executed. For example:
What is a function in Python?
A function in Python is a block of reusable code that performs a specific task. It is defined using the
How do you import a module in Python?
You import a module in Python using the
What is the purpose of the
How can you pass multiple arguments to a function?
Multiple arguments can be passed to a function by separating them with commas, for example,
What is a Python module? A Python module is a file containing Python code. It can include functions, classes, and variables and is used to organize code into manageable sections.
How do you access a function from a module?
You access a function from a module using dot notation, for example,
What is a linked list? A linked list is a linear data structure consisting of nodes, where each node contains a data field and a reference (or link) to the next node in the sequence.
How does a stack operate? A stack is a data structure that operates on a Last In, First Out (LIFO) principle. The last item added to the stack is the first one to be removed.
What is the time complexity for searching an element in a binary search tree (BST)? The average time complexity for searching an element in a binary search tree is O(log n), but it can degrade to O(n) in the worst case if the tree becomes unbalanced.
What is a queue and how does it function? A queue is a data structure that operates on a First In, First Out (FIFO) principle. Items are added at the rear and removed from the front.
Describe the main difference between an array and a linked list. An array is a fixed-size, indexed data structure that allows random access, whereas a linked list is a dynamic, sequential data structure where elements are linked via pointers.
What is a hash table, and how does it work? A hash table is a data structure that stores key-value pairs. It uses a hash function to compute an index into an array of buckets, where the desired value is found.
Explain what a tree data structure is. A tree is a hierarchical data structure consisting of nodes, with a single node designated as the root. Each node has zero or more child nodes, and nodes without children are called leaves.
What is the difference between a stack and a queue? A stack is a LIFO data structure, where the last element added is the first to be removed. A queue is a FIFO data structure, where the first element added is the first to be removed.
What is Object-Oriented Programming (OOP)? Object-Oriented Programming is a programming paradigm based on the concept of objects, which can contain data in the form of fields, often known as attributes, and code in the form of procedures, often known as methods.
What are the four main principles of OOP? The four main principles of OOP are Encapsulation, Abstraction, Inheritance, and Polymorphism.
Explain the concept of Encapsulation in OOP. Encapsulation is the concept of wrapping data and methods that operate on the data within a single unit, typically a class. It restricts direct access to some of the object's components and can prevent the accidental modification of data.
What is Inheritance in OOP? Inheritance is an OOP principle where a new class is created based on an existing class. The new class, known as a derived or child class, inherits attributes and methods from the existing class, known as a base or parent class.
How does Polymorphism work in OOP? Polymorphism allows objects of different classes to be treated as objects of a common superclass. It is achieved through method overriding where a child class provides a specific implementation of a method that is already defined in its parent class.
What is the purpose of the try-except block in Python? The try-except block in Python is used to handle exceptions or errors that may occur during the execution of the code. If an error occurs in the try block, the code in the except block is executed, allowing the programmer to manage the error gracefully without crashing the program.
How do you catch multiple exceptions using a single except block?
To catch multiple exceptions using a single except block, you can use a tuple of exception classes. For example:
# some code
except (ValueError, TypeError):
# handle errors
What is the purpose of the finally block in Python's exception handling? The finally block is used to define a set of actions that must be executed regardless of whether an exception occurs or not. It is placed after the try and except blocks and is useful for cleaning up resources, such as closing files or releasing external connections.
How can you raise an exception manually in Python?
You can raise an exception manually in Python using the raise statement. For example,
How do you open a file for reading in Python?
To open a file for reading in Python, use the
Which method is used to read the entire contents of a file as a string?
The
How do you ensure a file is properly closed after processing?
Use the
What is the purpose of the Python module 'os'? The 'os' module in Python provides a way of using operating system-dependent functionality such as reading or writing to the file system.
How do you import a specific function from a module in Python?
You can import a specific function from a module using the syntax:
What is the 'sys' module used for in Python? The 'sys' module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
How can you list all functions and variables defined in a module?
You can list all functions and variables defined in a module using the
What is the use of the 'random' module in Python? The 'random' module is used to generate pseudo-random numbers for various distributions, including integers, floating-point numbers, and sequences.
What is the purpose of using decorators in Python?
Decorators in Python are used to modify the behavior of a function or method. They allow for the extension of function behavior without modifying the function's code, often used for logging, enforcing access control, instrumentation, or caching. Syntax:
How does Python's Global Interpreter Lock (GIL) affect multithreading? The Global Interpreter Lock (GIL) in Python is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes simultaneously. This means that even in a multithreaded program, only one thread can execute Python code at a time. This can be a bottleneck for CPU-bound applications but does not affect I/O-bound or concurrent operations.
Explain the difference between deep copy and shallow copy in Python.
A shallow copy in Python creates a new object but inserts references to the original objects into the newly created object. A deep copy creates a new object and recursively adds copies of nested objects found in the original. Use
What is a metaclass in Python?
A metaclass in Python is a class that defines the behavior of other classes, essentially the class of a class. It allows for customizing class creation and is specified by setting the