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

Python 7 Marks Answers

This document provides an introduction to Python programming, covering its history, features, installation steps, data types, operators, and comparisons with other languages. It also includes control structures and loops with examples, such as if statements, for and while loops, and various programming exercises. The content is structured into units that systematically guide the reader through fundamental concepts and practical applications of Python.

Uploaded by

iec.vineet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Python 7 Marks Answers

This document provides an introduction to Python programming, covering its history, features, installation steps, data types, operators, and comparisons with other languages. It also includes control structures and loops with examples, such as if statements, for and while loops, and various programming exercises. The content is structured into units that systematically guide the reader through fundamental concepts and practical applications of Python.

Uploaded by

iec.vineet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

=== UNIT 1: Introduction to Python Programming ===

1. Explain the history and evolution of Python, including versions and creator
information:
Python was created by Guido van Rossum in the late 1980s and released in 1991. Its
early version, Python 0.9.0, included functions, modules, and exceptions. Python
2.x (2000) introduced Unicode support and list comprehensions. Python 3.x (2008)
fixed design flaws and included better Unicode handling and syntax improvements.
Python 3.10+ includes modern features like pattern matching and performance boosts.

2. Describe at least 8 features of Python with real-world advantages:


- Easy to learn and use: Beginner-friendly syntax.
- Interpreted language: Easier debugging.
- Dynamically typed: No need for variable declarations.
- Cross-platform: Code runs on multiple OS.
- Rich standard library: Built-in support for many functions.
- Object-oriented: Supports class-based programming.
- Community support: Active forums and packages.
- Extensible: Integrates with other languages like C/C++.

3. Explain the steps to install Python on Windows and configure it with VS Code or
IDLE:
- Download Python from python.org.
- Run installer and select "Add Python to PATH".
- Install VS Code from code.visualstudio.com.
- Open VS Code, install the Python extension.
- Set Python interpreter via Command Palette > "Python: Select Interpreter".
- Use IDLE directly from the Start Menu if preferred.

4. Program: Input name, age, city and print formatted:


```python
name = input("Enter name: ")
age = input("Enter age: ")
city = input("Enter city: ")
print(f"Hi, I'm {name}, {age} years old, from {city}.")
```

5. Discuss Python’s standard data types:


- Numbers: int (5), float (3.14), complex (2+3j)
- Strings: Immutable sequences of characters.
- Booleans: True or False values.
- Type functions: `type()` shows type; `isinstance()` checks type.

6. Explain arithmetic, relational, logical, and assignment operators:


- Arithmetic: +, -, *, /, %, //, **
- Relational: ==, !=, <, >, <=, >=
- Logical: and, or, not
- Assignment: =, +=, -=, *=, /=

7. Operator categories with examples:


- Arithmetic: a + b → 5
- Relational: a == b → True
- Logical: a and b
- Assignment: x += 2
- Membership: x in [1, 2]
- Identity: x is y

8. Program for input, arithmetic operations and type conversion:


```python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Division:", a / b)
print("Exponent:", a ** b)
```

9. Mutable vs Immutable:
- Mutable: Can be changed (lists, dicts).
- Immutable: Cannot be changed (strings, tuples).

10. Program for explicit type casting:


```python
i = 5
f = float(i)
s = str(f)
i2 = int(float(s))
print(i, f, s, i2)
```

11. Literal types:


- Numeric: int, float, complex
- String: "Hello"
- Boolean: True, False
- Special: None

12. Python vs C and Java:


- Syntax: Python is simpler and shorter.
- Memory: Python has automatic memory management.
- Use cases: Python is used in AI, Java in enterprise apps, C in system-level
programming.

=== UNIT 2: Control Structures and Loops ===

1. if, elif, else with age check:


```python
age = int(input("Enter age: "))
if age < 18:
print("Minor")
elif age < 60:
print("Adult")
else:
print("Senior")
```

2. Divisible by 5 and 11:


```python
num = int(input("Enter number: "))
if num % 5 == 0 and num % 11 == 0:
print("Divisible")
else:
print("Not divisible")
```

3. for vs while:
- for: Used with known ranges.
- while: Used with unknown conditions.
```python
for i in range(3): print(i)
i = 0
while i < 3:
print(i)
i += 1
```

4. Pattern program:
```python
for i in range(1, 4):
for j in range(1, i+1):
print(j, end=" ")
print()
```

5. Prime check:
```python
n = int(input("Enter number: "))
if n > 1:
for i in range(2, n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
```

6. break, continue, pass:


```python
# break
for i in range(5):
if i == 3:
break
print(i)

# continue
for i in range(5):
if i == 3:
continue
print(i)

# pass
for i in range(5):
if i == 3:
pass
print(i)
```

7. Menu-driven program:
```python
while True:
print("1.Add 2.Subtract 3.Exit")
choice = int(input("Enter choice: "))
if choice == 3:
break
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if choice == 1:
print("Sum:", a + b)
elif choice == 2:
print("Difference:", a - b)
```

8. Grade assignment using nested if:


```python
marks = int(input("Enter marks: "))
if marks >= 90:
grade = "A"
elif marks >= 75:
grade = "B"
elif marks >= 60:
grade = "C"
else:
grade = "D"
print("Grade:", grade)
```

9. Multiplication table:
```python
n = int(input("Enter number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
```

10. Sum of digits using while loop:


```python
n = int(input("Enter number: "))
total = 0
while n > 0:
total += n % 10
n //= 10
print("Sum of digits:", total)
```

11. range() usage:


```python
for i in range(5): print(i) # range(stop)
for i in range(2, 5): print(i) # range(start, stop)
for i in range(1, 10, 2): print(i) # range(start, stop, step)
```

12. Pattern using nested loops:


```python
for i in range(3):
for j in range(5):
print("*", end=" ")
print()
```

You might also like