0% found this document useful (0 votes)
2 views3 pages

Python Basics: Installation, Data Types, and More

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

Python Basics: Installation, Data Types, and More

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name = M.

Muzammil

Python Assignment

1. Who created Python, and what was the motivation behind its development?
Python was created by Guido van Rossum in the late 1980s and was released in 1991. His
motivation was to design a language that was easy to read, simple to use, and powerful
enough to handle complex programming tasks. He wanted a language that emphasized code
readability and allowed developers to write fewer lines of code compared to other
languages like C or Java.

2. Explain why Python has gained popularity in web development.


Python gained popularity in web development because of its simplicity and readability,
frameworks like Django and Flask, and vast community support with powerful libraries for
databases, APIs, and server-side logic.

3. What are the key differences between Python 2 and Python 3?


- Print statement: Python 2 → print "Hello", Python 3 → print("Hello")
- Division: Python 2 → 5/2 = 2, Python 3 → 5/2 = 2.5
- Unicode: Python 3 uses Unicode by default, Python 2 does not.
- Python 3 is supported, Python 2 is no longer maintained.

4. Define variables in Python. Give an example.


A variable is a reserved memory location used to store values. Example:

x = 10
name = "Krishna"

5. What are Python’s basic data types? Explain with examples.


- int: a = 5
- float: b = 3.14
- str: c = "Hello"
- bool: d = True
- list: e = [1, 2, 3]
- tuple: f = (1, 2, 3)
- dict: g = {"name": "Muzammil", "age": 17}

6. Describe the steps for installing Python 3.7+ and Anaconda.


1. Download Python 3.7+ from [Link] and install it.
2. Verify installation with python --version.
3. Download Anaconda from [Link].
4. Install it and verify with conda --version.

7. What is PIP, and how is it used in Python?


PIP is Python’s package installer, used to install and manage external libraries. Example: pip
install numpy

8. Write a Python program using an if-elif-else statement to check whether a


number is positive, negative, or zero.
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")

9. Differentiate between a for loop and a while loop in Python.


- for loop: Used when the number of iterations is known (e.g., iterating through a list).
- while loop: Used when iterations depend on a condition.

10. Write a simple loop to print the first 10 even numbers.


for i in range(2, 21, 2):
print(i)

11. Why are virtual environments important in Python projects?


They allow developers to create isolated spaces for projects, preventing conflicts between
dependencies and versions.

12. Compare Jupyter Notebook, PyCharm, and Spyder IDEs. Which would you
choose for data science projects and why?
- Jupyter Notebook: Great for data analysis and visualization.
- PyCharm: Full IDE for large projects with debugging.
- Spyder: Lightweight IDE for scientific computing.
For data science, Jupyter Notebook is preferred due to interactivity and visualization
support.

13. Define a function in Python. Write a function that returns the square of a
number.
def square(num):
return num * num
14. What is the difference between a function, module, and library in Python?
- Function: A block of code performing a task.
- Module: A file containing Python code.
- Library: A collection of modules.

15. Explain the purpose of Python modules. Give two examples.


Modules organize code into reusable pieces. Examples: math, random

16. What is the Python Standard Library? List three commonly used modules.
It is a collection of pre-installed modules in Python. Examples: math, os, datetime

17. How does the os module differ from the sys module in Python?
- os module: Deals with operating system tasks like file handling.
- sys module: Provides system-specific functions like interpreter details.

18. Explain exception handling in Python with an example using try-except.


try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("Cannot divide by zero.")

19. What is file handling in Python? Write a snippet to read and print a text file
line by line.
with open("[Link]", "r") as f:
for line in f:
print([Link]())

20. Why is exception handling critical in large-scale Python applications?


It prevents crashes, helps debugging, and provides meaningful error messages to users.

You might also like