Python Variables, Data Types & Keywords
Python Variables, Data Types & Keywords Interview Questions
What is a variable in Python?
A variable is a named location in memory used to store data. In Python, variables are created when you assign a value to them using the assignment operator (=). Example: x = 10, name = "John".
What is dynamic typing in Python?
Python is dynamically typed, meaning you don't need to declare the type of a variable. The type is determined at runtime based on the value assigned. Example: x = 5 (int), x = "hello" (str) - same variable can hold different types.
What are Python's naming conventions for variables?
Variable names should be descriptive, use lowercase letters with underscores (snake_case), avoid Python keywords, and cannot start with numbers. Example: student_name, total_count.
What are Python's built-in data types?
Python has several built-in data types: Numeric (int, float, complex), Boolean (bool), Sequence (str, list, tuple), Mapping (dict), Set (set, frozenset), and Binary types (bytes, bytearray).
What is the difference between mutable and immutable data types?
Mutable objects can be modified after creation (lists, dictionaries, sets). Immutable objects cannot be changed (strings, tuples, integers, floats). Example: Lists can be modified, but strings cannot.
What are Python keywords?
Keywords are reserved words in Python that have special meaning. They cannot be used as variable names. Python 3.9 has 35 keywords including: if, else, for, while, def, class, import, from, True, False, None.
How to check the type of a variable in Python?
Use the type() function. Example: type(10) returns <class 'int'>, type("hello") returns <class 'str'>.
What is None in Python?
None is a special constant representing the absence of a value or a null value. It's an object of its own datatype (NoneType). It's different from 0, False, or empty string.
What is type conversion in Python?
Type conversion (type casting) is converting one data type to another. Implicit conversion: Python automatically converts (int to float). Explicit conversion: Using functions like int(), float(), str(), list(), etc.
What is the difference between list and tuple?
Lists are mutable (can be modified), defined with square brackets []. Tuples are immutable (cannot be modified), defined with parentheses (). Lists have more built-in methods than tuples.
What is a dictionary in Python?
A dictionary is an unordered collection of key-value pairs, defined with curly braces {}. Keys must be unique and immutable (strings, numbers, tuples). Values can be of any type. Example: {"name": "John", "age": 25}.
What is a set in Python?
A set is an unordered collection of unique elements, defined with curly braces {} or set(). Sets don't allow duplicates and support mathematical set operations (union, intersection, difference).
What are local and global variables?
Local variables are defined inside a function and can only be accessed within that function. Global variables are defined outside functions and can be accessed throughout the program. Use global keyword to modify global variables inside functions.
What is the id() function in Python?
The id() function returns the identity (unique integer) of an object. This identity is the object's memory address. Example: id(x) returns the memory address of variable x.
What is the difference between == and is operators?
== compares the values of two objects. is compares the identities (memory addresses) of two objects. Example: a == b checks if values are equal, a is b checks if they are the same object.
What are Python literals?
Literals are raw data given in variables or constants. Types include: Numeric literals (integers, floats, complex), String literals (single, double, triple quotes), Boolean literals (True, False), and Special literal (None).
What is string slicing in Python?
String slicing extracts parts of a string using the syntax string[start:stop:step]. Example: "Python"[0:3] returns "Pyt", "Hello"[::-1] returns "olleH" (reverse).
What is the del keyword in Python?
The del keyword is used to delete objects, variables, or items from collections. Example: del x deletes variable x, del my_list[2] deletes item at index 2.
What are Python's numeric data types?
Python has three numeric types: Integer (int) - whole numbers, Float (float) - decimal numbers, and Complex (complex) - numbers with real and imaginary parts (e.g., 3+5j).
How to create multi-line strings in Python?
Use triple quotes (single or double) to create multi-line strings. Example: text = """This is a
multi-line
string.""" Preserves line breaks and formatting.
Note: These questions cover fundamental concepts of Python variables, data types, and keywords. Understanding these is crucial for Python programming interviews as they form the foundation of the language. Remember that Python is dynamically typed and uses automatic memory management.