Python Assignment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Hassan Abubakar Ahmad

Enrollment No: R-14785

Assignment — Mobile App using android

Q1) Explain the role of the Python interpreter and interactive mode in Python
programming.

The Python interpreter is a program that reads and executes Python code. It provides an
interactive mode, also known as the REPL (Read-Eval-Print Loop), where users can write
and execute Python code line by line.

The interactive mode allows users to:

- Write and execute Python code immediately.

- Test and debug code snippets.

- Experiment with new ideas and concepts.

The interpreter also provides features like syntax highlighting, code completion, and
error messages.

Q2) Differentiate between variables and identifiers in Python with examples.

Identifiers: Names given to variables, functions, classes, and other objects in Python.
Identifiers must follow specific naming conventions, such as starting with a letter or
underscore.

Variables: Storage locations that hold values. Variables are associated with identifiers,
which are used to access and manipulate the stored values.
Example:

x = 5 # 'x' is an identifier, and the variable 'x' holds the value 5.

Q3) Discuss the difference between dynamic typing and strong typing in Python,
providing relevant examples.

Python is a dynamically-typed language, which means:

Dynamic Typing: The data type of a variable is determined at runtime, not at compile
time. This allows for more flexibility in coding.

Strong Typing: Python is strongly-typed, meaning it enforces strict type constraints. This
prevents implicit type conversions and ensures type safety.

Example:

x = 5 # 'x' is an integer

x = "hello" # 'x' is now a string

In this example, the variable 'x' changes its type from integer to string at runtime.

Q4) Describe the functionality of the type() function and the is operator in Python. How
are they used to determine types?

The type() function and the is operator are used to determine the type of an object in
Python:

type() function: Returns the type of an object as a type object.

is operator: Checks if an object is an instance of a particular type.

Example:

x=5

print(type(x)) # Output: <class 'int'>

print(isinstance(x, int)) # Output: True

Q5) Explain the purpose and structure of an if-else statement in Python. Provide an
example.

The if-else statement in Python is used to execute different blocks of code based on
conditions:

if clause: Specifies the condition to be evaluated.

else clause: Specifies the code to be executed if the condition is false.

Example:

x=5

if x > 10:

print("x is greater than 10")


else:

print("x is less than or equal to 10")

Q6) Write a Python program using a while loop that prints the numbers from 1 to 10.
Include a brief explanation of the loop structure.

i=1

while i <= 10:

print(i)

i += 1

Explanation:

- The while loop continues to execute as long as the condition (i <= 10) is true.

- The loop variable 'i' is initialized to 1 before the loop starts.

- Inside the loop, the current value of 'i' is printed, and then 'i' is incremented by 1.

Q7) What is the difference between break and continue statements in Python? Provide a
code example to illustrate their use.

The break and continue statements in Python are used to control the flow of loops:

break statement: Terminates the loop entirely and transfers control to the statement
immediately after the loop.
continue statement: Skips the current iteration and moves on to the next iteration.

Example:

for i in range(1, 11):

if i == 5:

break

print(i)

for i in range(1, 11):

if i == 5:

continue

print(i)

Example:

def greet(name, message="Hello"):

print(f"{message}, {name}!")

greet("John") # Output: Hello, John!

greet("John", "Hi") # Output: Hi, John!

def greet(name, message="Hello"):


print(f"{message}, {name}!")

greet(name="John", message="Hi") # Output: Hi, John!

greet(message="Hi", name="John") # Output: Hi, John!

Q9) Explain the concept of a dictionary in Python, including its syntax, methods, and use
cases.

In Python, a dictionary is an unordered collection of key-value pairs:

Syntax: `dictname = {"key1": value1, "key2": value2, ...}`

Methods: `keys()`, `values()`, `items()`, `get()`, `update()`, `pop()`, etc.

Use cases: Dictionaries are useful for storing and manipulating data that requires fast
lookups, such as caching, configuration files, and data processing.

Example:

person = {"name": "John", "age": 30, "city": "New York"}

print(person["name"]) # Output: John

person["country"] = "USA"

print(person) # Output: {"name": "John", "age": 30, "city": "New York", "country": "USA"}

You might also like