Title: Python Programming — LearnCoachAssist

Q: What is Python?
A: Python is a high-level, interpreted programming language known for its readability and simplicity. It supports multiple paradigms including procedural, object-oriented, and functional programming.

Q: Who created Python and when?
A: Guido van Rossum created Python, with the first version released in 1991. It was named after the British comedy series Monty Python.

Q: What are the key features of Python?
A: Python features dynamic typing, automatic memory management, extensive standard library, and cross-platform compatibility. It emphasizes code readability with significant whitespace.

Q: How do you run a Python script from the command line?
A: Save the code in a file with .py extension, then run python filename.py in the terminal. Use python3 if Python 2 is also installed.

Q: What is the Python REPL?
A: REPL stands for Read-Eval-Print Loop, an interactive shell for executing Python code line-by-line. Access it by typing python in the terminal.

Q: What are variables in Python?
A: Variables are names that reference objects storing data. They are created on first assignment, like x = 5, and are dynamically typed.

Q: Explain dynamic typing in Python.
A: Dynamic typing means variable types are determined at runtime, not compile-time. A variable can hold different types sequentially, e.g., x = 5; x = 'hello'.

Q: What are the basic data types in Python?
A: Basic immutable types include int (integers), float (floating-point), str (strings), and bool (True/False). Mutable types include lists and dictionaries.

Q: What is the value of None in Python?
A: None is a singleton object representing the absence of a value, often used as a default return for functions without explicit returns.

Q: How do you convert data types in Python?
A: Use functions like int(), float(), str(), or bool(). For example, int('42') converts string '42' to integer 42.

Q: What are arithmetic operators in Python?
A: They include + (addition), - (subtraction), * (multiplication), / (division), // (floor division), % (modulo), and ** (exponentiation).

Q: What do comparison operators return?
A: Comparison operators like ==, !=, >, <, >=, <= return a boolean value: True or False.

Q: What are logical operators in Python?
A: Logical operators are and (both true), or (either true), and not (negation). They short-circuit evaluate.

Q: How does the print() function work?
A: print() outputs objects to stdout. It converts them to strings, with sep=' ' between arguments and end='\n' at the end.

Q: What does input() return?
A: input(prompt) reads a line from stdin, strips the newline, and returns it as a string. Use int() or float() to convert.

Q: How do you write an if statement?
A: Use if condition: followed by indented block. Condition evaluates to boolean; non-empty/non-zero is truthy.

Q: What are elif and else?
A: elif is else-if for additional conditions; else executes if no prior conditions match. They chain after if.

Q: How does a while loop work?
A: while condition: repeats indented block while condition is truthy. Use with caution to avoid infinite loops.

Q: Explain the for loop in Python.
A: for item in iterable: iterates over sequences like lists or range(). range(n) generates 0 to n-1.

Q: What do break, continue, and pass do?
A: break exits loop; continue skips to next iteration; pass is a no-op placeholder.

Q: How do you define a function?
A: Use def function_name(parameters): followed by indented body. Call with function_name(args).

Q: What is the difference between parameters and arguments?
A: Parameters are variables in function definition; arguments are values passed when calling. Keyword arguments use name=value.

Q: What does the return statement do?
A: return value exits function and sends value back. Without it, returns None.

Q: How do default parameters work?
A: Define as def func(param=default):. Caller can omit if using default.

Q: What are *args and **kwargs?
A: *args packs extra positional args as tuple; **kwargs packs keyword args as dict.

Q: How do you create and access lists?
A: Lists are mutable: mylist = [1, 2, 3]. Access via index: mylist[0]; negative for reverse.

Q: What are common list methods?
A: Methods include append(x), pop(), insert(i,x), remove(x), sort(), and reverse().

Q: What are tuples?
A: Tuples are immutable sequences: t = (1, 2, 3). Created with comma or parentheses; useful for fixed data.

Q: How do dictionaries work?
A: Dicts map keys to values: d = {'key': 'value'}. Keys are immutable; access with d['key'].

Q: What are sets in Python?
A: Sets are unordered collections of unique elements: s = {1, 2}. Support add(), remove(), union, intersection.

Q: How do you slice strings?
A: Strings are immutable: s[1:3] gets substring from index 1 to 3 (exclusive). Use step: s[::2].

Q: Name key string methods.
A: Methods: upper(), lower(), strip(), split(), join(), find(), replace().

Q: What are f-strings?
A: F-strings format strings: f'Hello {name}'. Introduced in Python 3.6 for efficient interpolation.

Q: How do you open and read a file?
A: with open('file.txt', 'r') as f: then f.read() or f.readlines(). 'with' auto-closes.

Q: How do you write to a file?
A: with open('file.txt', 'w') as f: f.write('text'). 'w' overwrites; 'a' appends.

Q: How do you define a class?
A: class MyClass: followed by methods. Instances created with obj = MyClass().

Q: What is the __init__ method?
A: def __init__(self, args): is constructor called on instance creation. 'self' refers to instance.

Q: What are instance vs class variables?
A: Instance vars unique per object (self.var); class vars shared (MyClass.var).

Q: How do methods work in classes?
A: Methods take 'self' first: def method(self):. Called as obj.method().

Q: What is inheritance in Python?
A: Subclass: class Child(Parent):. Child inherits methods; override with same name.

Q: How do you import modules?
A: import module or from module import func. Standard lib: math, os, sys.

Q: What is a package?
A: Package is directory with __init__.py containing modules. Import as from pkg.module import func.

Q: How does exception handling work?
A: try: ... except ValueError: ... else: ... finally: .... Catches specific exceptions.

Q: How do you raise an exception?
A: raise ValueError('message'). Used for custom error conditions.

Q: What are lambda functions?
A: Anonymous functions: lambda x: x*2. Single expression, often for map(), filter(), sorted().

Q: What are list comprehensions?
A: Concise lists: [x*2 for x in range(5) if x%2==0]. Faster and readable than loops.

Q: Explain generators.
A: Generators yield values lazily: def gen(): yield 1. Use next() or for loop; saves memory.

Q: What are decorators?
A: Functions wrapping others: @decorator def func():. Modify behavior without changing code, e.g., @staticmethod.

Q: What is a context manager?
A: Manages resources: with open() as f:. Custom via class with __enter__ __exit__ or @contextmanager.

Q: What are async and await?
A: Asyncio for concurrency: async def func(): await asyncio.sleep(1). Runs non-blocking I/O.