0% found this document useful (0 votes)
3 views4 pages

Python Basics Terminal Questions

Uploaded by

pankaj singh
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)
3 views4 pages

Python Basics Terminal Questions

Uploaded by

pankaj singh
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

DCA2205: PYTHON PROGRAMMING

Unit 2: Python Basics


Answers to Terminal Questions

Q1. What is an expression in Python, and how does it differ from a statement?

An expression is a combination of values, variables, operators, and function calls that Python evaluates to
produce a single value. If you can print it or assign it to a variable, it is an expression.

A statement is a complete instruction that the Python interpreter executes to perform an action (like
creating a variable, repeating a loop, or branching paths). It does not inherently return or evaluate to a
value.

Feature Expression Statement

Purpose Calculates/produces a final value. Executes a command or controls the


program flow.

Evaluation Always evaluates to something. Does not yield a value directly.

Examples 5 + 3 x = 10 (Assignment)
x * 2 if x > 5: (Conditional)
len("Python") import math

Q2. Differentiate between literals and constants with examples.


• Literals: Raw data values provided directly in a program's source code. They represent fixed values
that cannot be altered.
Examples: 42 (Integer literal), 3.14 (Float literal), "Hello" (String literal), True (Boolean literal).
• Constants: Variables whose values are intended to remain unchanged throughout the program's
execution. Python does not have a built-in strict constant type, so developers use uppercase naming
conventions to signify constants.
Examples: PI = 3.14159 , MAX_CONNECTIONS = 100 .

DCA2205: Python Programming - Unit 2 Basics 1


Q3. Explain simple and compound expressions with examples.
• Simple Expressions: Basic building blocks containing either a single literal value, a single variable, or a
single operator connecting two operands.
Examples: 10 , x , a + b .
• Compound Expressions: Formed by combining multiple simple expressions using operators,
parentheses, or nested function calls. They contain multiple sub-expressions and follow rules of
operator precedence.
Examples: (a + b) * (c / d) , x > 5 and y < 10 .

Q4. What are Python’s built-in numeric data types, and where are they used?

Python provides three core built-in numeric types:

1. Integer (int): Represents whole numbers without fractional parts (positive, negative, or zero).
Usage: Counting items, indexing sequences, loop iterations (e.g., items = 10 ).
2. Floating-Point (float): Represents real numbers containing a decimal point or fractional part.
Usage: Measuring precise metrics like currency, weights, temperature, or scientific computations (e.g.,
price = 99.95 ).

3. Complex (complex): Consists of a real and an imaginary part, written as real + imag j .
Usage: Advanced engineering, physics calculations, electrical networks, and signal processing (e.g., z
= 2 + 3j ).

Q5. Differentiate between mutable and immutable data types with examples.

The difference lies in whether an object's state or content can be changed after it is created in memory.

Property Mutable Data Types Immutable Data Types

Definition Objects whose values can be Objects whose values cannot be


modified in place without changing changed after creation. Any
their identity (memory address). modification creates a completely
new object.

Examples list , dict , set int , float , str , tuple , bool

Code Sample
my_list = [1, 2] my_str = "Hi"
my_list[0] = 99 # my_str[0] = "b" # Raises
Allowed TypeError

DCA2205: Python Programming - Unit 2 Basics 2


Q6. What is the difference between implicit and explicit type conversion?
• Implicit Type Conversion (Coercion): Automatically performed by the Python interpreter without any
user intervention. It safely converts a smaller data type to a wider data type to avoid data loss.
Example: Adding an integer to a float: 5 + 2.0 automatically evaluates to the float 7.0 .
• Explicit Type Conversion (Type Casting): Manually performed by the programmer using built-in
constructor functions to explicitly force an object to a new type.
Example: Converting a string to an integer: int("100") or converting a float to an integer: int(5.9)
(which truncates to 5 ).

Q7. State the rules for naming variables in Python.

When defining identifiers/variables in Python, the following strict structural rules apply:

1. Variables must begin with either a letter (a-z, A-Z) or an underscore ( _ ). They cannot start with a digit.
2. The remaining characters can be letters, numbers, or underscores ( a-z, A-Z, 0-9, _ ). Special
symbols like @ , $ , % are strictly prohibited.
3. Variable names are case-sensitive (e.g., age , Age , and AGE are three distinct variables).
4. Python keywords or reserved words (such as if , else , while , def , class ) cannot be used as
variable names.

Q8. What is the difference between is and == operators in Python?


• == Operator (Equality Operator): Compares the actual values of the two operands to check if they are
equal.
Example: If a = [1, 2] and b = [1, 2] , a == b evaluates to True .
• is Operator (Identity Operator): Compares the actual memory addresses (identities) of the objects.
It checks if both variables point to the exact same object in memory.
Example: Following the statement above, a is b evaluates to False because they are distinct list
allocations in memory.

Q9. Explain the role of assignment operators with examples.

Assignment operators are used to store values into variables. Beyond the basic assignment operator ( = ),
Python supports compound/augmented assignment operators which combine an arithmetic or bitwise
operation with value assignment in a concise step.

• = (Simple Assignment): x = 5 (Assigns 5 to x)


• += (Add and Assign): x += 3 (Equivalent to x = x + 3 )
• -= (Subtract and Assign): x -= 2 (Equivalent to x = x - 2 )
• *= (Multiply and Assign): x *= 4 (Equivalent to x = x * 4 )
• /= (Divide and Assign): x /= 2 (Equivalent to x = x / 2 )

DCA2205: Python Programming - Unit 2 Basics 3


Q10. What is operator precedence, and why is it important in Python?

Operator Precedence defines the execution order of operators when a complex compound expression
contains more than one operator. It works similarly to the standard mathematical rule BODMAS/PEMDAS.

Importance: It ensures that expressions are evaluated unambiguously and consistently. Without
predefined operator precedence, the same line of code could yield completely different results, leading to
logic flaws and unpredictable behavior.

For example, in the expression x = 5 + 3 * 2 , the multiplication operator ( * ) has higher precedence
than addition ( + ). Therefore, 3 * 2 evaluates first to 6 , and then 5 + 6 evaluates to 11 . Parentheses
() can be used to override this precedence hierarchy manually.

DCA2205: Python Programming - Unit 2 Basics 4

You might also like