0% found this document useful (0 votes)
31 views6 pages

Python Core Concepts for Interviews

This document provides detailed notes on core Python concepts relevant for interviews, covering variables, data types, operators, conditionals, loops, functions, data structures, OOP principles, file handling, exception handling, and modules. It includes examples and explanations of key features such as lists, tuples, sets, dictionaries, and various built-in functions. The notes conclude with a mention of advanced topics like NumPy, Pandas, and Machine Learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Python Core Concepts for Interviews

This document provides detailed notes on core Python concepts relevant for interviews, covering variables, data types, operators, conditionals, loops, functions, data structures, OOP principles, file handling, exception handling, and modules. It includes examples and explanations of key features such as lists, tuples, sets, dictionaries, and various built-in functions. The notes conclude with a mention of advanced topics like NumPy, Pandas, and Machine Learning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Detailed Python Core Concepts Notes for Interviews (LSEG-Focused)

🔹 PART 1: VARIABLES, DATA TYPES, OPERATORS


Variables & Data Types
• No explicit declaration needed (Python is dynamically typed)
• Common types: int , float , str , bool , list , tuple , set , dict
• Type conversion: int() , float() , str()
• input() for user input, returns string by default

Operators
• Arithmetic: + , - , * , / , // , % , **
• Comparison: == , != , > , < , >= , <=
• Logical: and , or , not
• Membership: in , not in

🔹 PART 2: CONDITIONALS & LOOPS


Conditional Statements

if condition:
...
elif condition:
...
else:
...

Loops
• for loops: Iterates over sequences
• while loops: Repeats until condition is false
• break , continue , pass

1
🔹 PART 3: FUNCTIONS
Function Basics

def function_name(parameters):
return result

• Default arguments: def greet(name, lang='English')


• *args : Variable positional args (tuple)
• **kwargs : Variable keyword args (dict)

Lambda Functions

square = lambda x: x * x

Used with map() , filter() , sorted()

🔹 PART 4: PYTHON DATA STRUCTURES


List
• Mutable, ordered, allows duplicates

lst = [1, 2, 3]
[Link](4)
lst[0]

Tuple
• Immutable, ordered

tpl = (1, 2)
tpl[1]

Set
• Unordered, no duplicates

2
s = {1, 2, 3}
[Link](4)

Dictionary
• Key-value pairs

d = {"a": 1, "b": 2}
d["a"]

🔹 PART 5: STACK, QUEUE, LINKEDLIST, HEAP


Stack (LIFO)

stack = []
[Link](1)
[Link]()

Queue (FIFO)

from collections import deque


queue = deque()
[Link]('A')
[Link]()

Linked List (Singly)

class Node:
def __init__(self, data):
[Link] = data
[Link] = None

3
Heap

import heapq
[Link](list)
[Link]()

🔹 PART 6: ENUMERATE, ZIP, MAP, FILTER, SORTED


• enumerate() returns (index, value)
• zip() combines two lists
• map(func, iterable) applies function
• filter(func, iterable) filters elements
• sorted(list, key=lambda) sorts with logic

🔹 PART 7: OOPS IN PYTHON


4 Pillars

1. Encapsulation

• Hide data with private variables ( __var )


• Access via methods

2. Abstraction

• Hide implementation details


• Use abc module and @abstractmethod

3. Inheritance

• One class inherits from another

class A:
...
class B(A):
...

4. Polymorphism

• Same function behaves differently


• Method overriding and duck typing

4
Special Methods
• __init__ : Constructor, runs on object creation
• __str__ : Defines print behavior of object

🔹 PART 8: FILE HANDLING

with open("[Link]", "r") as f:


data = [Link]()

Modes: 'r' , 'w' , 'a' , 'x' , 'b'

Write

with open("[Link]", "w") as f:


[Link]("Hello")

Read line by line

for line in f:
print([Link]())

🔹 PART 9: EXCEPTION HANDLING

try:
...
except ValueError:
...
finally:
...

• try , except , finally


• Custom exceptions using class inheritance from Exception

5
🔹 PART 10: MODULES
• Use import module_name
• Built-ins: math , random , datetime , os , sys
• Custom module: Create .py file and import

End of Core Python Notes

Next: NumPy, Pandas, Matplotlib, and Machine Learning

Common questions

Powered by AI

Polymorphism in Python allows functions to operate on objects of different classes if they implement a method with the same name. Method overriding, a station of polymorphism, lets a subclass provide a specific implementation for a method already defined in its superclass. This ensures the subclass can alter the method’s behavior specific to it while maintaining the interface .

Modules in Python allow for the organization and reuse of code across different scripts. Built-in modules like math and os provide additional functionality. To import a custom module, save the code as a .py file and use 'import module_name' in another script. This promotes modularity and reduces code duplication .

Opening a file in write ('w') mode truncates the file to zero length before writing, removing existing content, whereas append ('a') mode writes data to the end of the file, preserving existing content. Use write when overwriting is needed, and append when augmenting the file with new data is desired .

The 'try' block contains code that might throw an exception. The 'except' block captures exceptions, allowing for graceful error handling. The 'finally' block contains code that should run regardless of whether an exception occurred. Example: 'try: risky_code() except ValueError: handle_error() finally: cleanup()'. This allows handling only specific errors and ensures cleanup happens .

The four pillars of Object-Oriented Programming in Python are Encapsulation, Abstraction, Inheritance, and Polymorphism. Encapsulation is implemented by using private variables (prefix __) to prevent direct access and modifying them through methods. For example, a class might have a private variable '__data' and a method 'get_data()' to retrieve its value .

The 'map' function is appropriate for applying a single function to each item in an iterable, like when transforming a list of numbers. 'filter' is suitable when a subset of items needs to be extracted from an iterable based on a condition. 'sorted' should be used to create a sorted representation, such as ordering records by a specific key value .

Lambda functions in Python are anonymous functions defined with the lambda keyword. They can have any number of inputs but only one expression. The result of the expression is returned. They're often used for throwaway functions, especially when combined with map(), filter(), and sorted(). For example, 'square = lambda x: x * x' defines a simple lambda function to square a number .

Python has 'for' loops, which iterate over sequences, and 'while' loops, which repeat until a condition is false. 'for' loops are suitable for iterating over collections like lists or strings, while 'while' loops are better for scenarios where the number of iterations depends on a condition — like a countdown or reading from a file until EOF .

The primary differences are that lists are mutable, allowing changes in situ, while tuples are immutable and cannot be altered after creation. Lists are used when elements are expected to change, while tuples are suited for static collections where immutability is desired for safety or performance reasons .

Python is dynamically typed, meaning there is no need for explicit declaration of variables. The common types used include int, float, str, bool, list, tuple, set, and dict.

You might also like