0% found this document useful (0 votes)
95 views6 pages

Python Basics and Sample Programs

The document provides an overview of Python, highlighting its features such as being easy to learn, interpreted, and object-oriented. It includes various applications of Python, examples of basic programming tasks, and explanations of fundamental concepts like tokens, variables, data types, and control structures. Additionally, it contains sample Python programs for arithmetic operations, area calculations, number checks, and loops.

Uploaded by

hegdea645
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)
95 views6 pages

Python Basics and Sample Programs

The document provides an overview of Python, highlighting its features such as being easy to learn, interpreted, and object-oriented. It includes various applications of Python, examples of basic programming tasks, and explanations of fundamental concepts like tokens, variables, data types, and control structures. Additionally, it contains sample Python programs for arithmetic operations, area calculations, number checks, and loops.

Uploaded by

hegdea645
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

1. What is Python? List some features of Python.

Python is a high-level, interpreted programming language known for its simplicity and readability.

Features:

- Easy to learn and use

- Interpreted language

- Dynamically typed

- Object-oriented

- Large standard library

- Open-source and community-developed

2. List any five applications of Python.

1. Web Development

2. Data Science and Machine Learning

3. Automation/Scripting

4. Game Development

5. Desktop GUI Applications

3. Explain how Python is interpreted.

Python code is executed line-by-line using an interpreter. It converts source code into bytecode and

then executes it using the Python Virtual Machine (PVM).

4. Define tokens. List different types of tokens in Python.

Tokens are the smallest units of a Python program.

Types:

- Keywords

- Identifiers

- Literals
- Operators

- Punctuators (delimiters)

5. Write a Python program to print 'Python is easy'.

print("Python is easy")

6. Write a Python program to perform the arithmetic operations.

a = 10

b=5

print("Addition:", a + b)

print("Subtraction:", a - b)

print("Multiplication:", a * b)

print("Division:", a / b)

7. Write a Python program to find the area of a circle.

r = float(input("Enter radius: "))

area = 3.14159 * r * r

print("Area of circle:", area)

8. Write a Python program to swap two numbers.

a=5

b = 10

a, b = b, a

print("a:", a, "b:", b)

9. Write a Python program to find the square and cube of a number.

num = int(input("Enter a number: "))


print("Square:", num**2)

print("Cube:", num**3)

10. Explain variables and data types in Python.

Variables are containers for storing data values.

Data Types:

- int (e.g., 10)

- float (e.g., 10.5)

- str (e.g., 'hello')

- bool (True/False)

- complex (e.g., 3+4j)

11. Explain the if-elif-else ladder with an example.

num = int(input("Enter a number: "))

if num > 0:

print("Positive")

elif num == 0:

print("Zero")

else:

print("Negative")

12. Write a Python program to check whether a number is even or odd.

num = int(input("Enter a number: "))

if num % 2 == 0:

print("Even")

else:

print("Odd")
13. Write a Python program 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("Zero")

else:

print("Negative")

14. Write a Python program to check if a year is a leap year.

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

print("Leap year")

else:

print("Not a leap year")

15. Write a Python program to find the largest of three numbers.

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

if a >= b and a >= c:

print("Largest:", a)

elif b >= a and b >= c:

print("Largest:", b)

else:

print("Largest:", c)
16. Explain while loop with an example.

i=1

while i <= 5:

print(i)

i += 1

17. Write a Python program to print numbers from 1 to 10 using while loop.

i=1

while i <= 10:

print(i)

i += 1

18. Write a Python program to print even numbers between 1 and 20 using while loop.

i=2

while i <= 20:

print(i)

i += 2

19. Write a Python program to display the multiplication table of a number.

num = int(input("Enter a number: "))

i=1

while i <= 10:

print(f"{num} x {i} = {num*i}")

i += 1

20. Write a Python program to print factorial of a number using while loop.

num = int(input("Enter a number: "))


fact = 1

i=1

while i <= num:

fact *= i

i += 1

print("Factorial:", fact)

Common questions

Powered by AI

Python is extensively used for automation and scripting, which streamlines repetitive tasks such as file manipulation, data entry, and system monitoring . It offers several advantages over manual processes, including reduced human error, increased efficiency, and time savings. The simplicity and readability of Python's syntax make it accessible for writing scripts that automate complex workflows, allowing consistent and repeatable task execution.

Python's large standard library provides modules and functions for many tasks such as file I/O, system calls, and networking, which enhances usability by allowing both beginners and professionals to implement functionalities without writing custom code from scratch . This comprehensive support simplifies learning and application development, reducing the need for external libraries and making the language accessible and practical for a wide range of applications.

Python has become a cornerstone in data science and machine learning due to its comprehensive libraries like Pandas for data manipulation, NumPy for numerical operations, and scikit-learn for machine learning . These libraries streamline complex data analysis and model development tasks. Additionally, Python's open-source nature and a large community contribute to constant improvements and a robust support system, making it a preferred choice for data scientists and developers worldwide.

Python, being an interpreted language, executes code line-by-line using an interpreter. This means it converts the source code into bytecode, which is then executed by the Python Virtual Machine (PVM). In contrast, compiled languages convert the entire source code into machine code before execution, which can lead to faster execution times since this process is done once. However, Python's interpreted nature simplifies the development process by allowing for dynamic typing and more flexibility in testing and debugging.

Python's status as an open-source language means it is freely available and can be modified, driven by a large and active community that continually contributes to its development . This facilitates innovation through the rapid integration of new features and bug fixes. The extensive community support ensures abundant resources and forums for troubleshooting, which encourages its use by reducing barriers to learning and implementing Python across different industries.

Python's data types, including int, float, str, bool, and complex, allow for effective storage and manipulation of data . Variables in Python can hold different data types at different times due to dynamic typing, providing flexibility and enabling developers to write concise and efficient code. This adaptability is particularly useful in applications requiring frequent data type adjustments, like user input processing or dynamic data manipulation.

Python's object-oriented features promote code reuse and better organization through the use of classes and objects . Classes act as blueprints for creating objects, encapsulating data for the object and functions to manipulate that data. This leads to more modular code where classes can be reused across different programs, reducing redundancy. Inheritance allows new classes to be based on existing ones, further enhancing code reuse by enabling the extension of existing functionality without altering existing code.

Being dynamically typed means that Python determines the type of a variable at runtime, which simplifies coding by eliminating the need for explicit variable declarations . However, this flexibility can lead to performance inefficiencies as type checking happens during execution. Furthermore, errors related to type can only be detected at runtime, potentially making bug detection more challenging compared to statically typed languages where such errors can be caught during compilation.

A year is a leap year if it is divisible by 4, except for end-of-century years, which must be divisible by 400 . In Python, this can be checked using conditional statements: if the year modulo 4 equals 0 and either not divisible by 100 or divisible by 400, it is a leap year. This logic efficiently determines the leap year status using nested conditions to check for the remainder when divided by 4, 100, and 400.

While loops in Python are effective for tasks requiring repeated execution until a condition is met, such as printed number sequences or calculating factorials . They are simple to implement and useful in scenarios where the number of iterations is not predetermined. However, for complex scenarios involving nested conditions or when iteration count is known, for loops might be more readable and efficient. While loops can lead to condition-execution errors like infinite loops if not carefully managed, emphasizing the importance of proper loop control in complex tasks.

You might also like