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

Python Basic Programs With Explanation

The document provides a collection of basic Python programs along with explanations for each. It covers fundamental concepts such as printing output, user input, data types, conditional statements, loops, and various data structures like lists, tuples, sets, and dictionaries. Each program is designed to illustrate specific Python functionalities and syntax.
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)
1 views3 pages

Python Basic Programs With Explanation

The document provides a collection of basic Python programs along with explanations for each. It covers fundamental concepts such as printing output, user input, data types, conditional statements, loops, and various data structures like lists, tuples, sets, and dictionaries. Each program is designed to illustrate specific Python functionalities and syntax.
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

PYTHON BASIC PROGRAMS WITH EXPLANATION

UNIT – 1 : Basic Python Programs


1. Print Hello World
print("Hello, World!")
Explanation: This program prints the text 'Hello, World!' on the screen. It is the first step to
understand Python syntax and output.

2. Input and Output Example


name = input("Enter your name: ")
print("Hello,", name)
Explanation: This program takes user input using the input() function and displays it along with
a greeting message.

3. Area of a Circle
r = float(input("Enter radius: "))
area = 3.14159 * r * r
print("Area of Circle =", area)
Explanation: It reads the circle's radius and computes the area using the formula πr². The result
is printed as output.

4. Data Type Checking


x = 10
y = 10.5
z = "Vishal"
print(type(x), type(y), type(z))
Explanation: This program uses the type() function to check the data type of each variable:
integer, float, and string.

5. Operator Example
a = 10
b = 3
print("Addition:", a + b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Bitwise AND:", a & b)
Explanation: This program demonstrates arithmetic, floor division, and bitwise operations using
Python operators.

UNIT – 2 : Conditional and Core Data Type Programs


1. Check Positive, Negative, or Zero
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
Explanation: Uses if-elif-else conditions to determine whether a number is positive, negative,
or zero.

2. Check Even or Odd


n = int(input("Enter number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Explanation: Checks if a number is divisible by 2. If yes, it's even; otherwise, it's odd.

3. Sum of First N Natural Numbers


n = int(input("Enter n: "))
sum = 0
for i in range(1, n+1):
sum += i
print("Sum =", sum)
Explanation: This program calculates the sum of first n natural numbers using a for loop.

4. Factorial Using While Loop


n = int(input("Enter number: "))
fact = 1
i = 1
while i <= n:
fact *= i
i += 1
print("Factorial =", fact)
Explanation: Calculates the factorial by multiplying numbers from 1 to n using a while loop.

5. List Example
fruits = ["apple", "banana", "cherry"]
print(fruits[1])
[Link]("orange")
print(fruits)
print(fruits[1:3])
Explanation: Demonstrates list operations like indexing, appending elements, and slicing.

6. Tuple Example
t = (10, 20, 30)
print(t[1])
Explanation: Shows tuple creation and indexing. Tuples are immutable and store ordered data.
7. Set Example
s = {1, 2, 3, 2, 1}
print(s)
[Link](4)
print(s)
Explanation: Demonstrates that sets store unique elements and support add() operation.

8. Dictionary Example
student = {"name": "Vishal", "age": 22, "course": "CSE"}
print(student["name"])
student["age"] = 23
print(student)
Explanation: Shows creation, accessing, and updating dictionary elements.

9. Nested Dictionary Example


college = {
"student1": {"name": "Vishal", "marks": 85},
"student2": {"name": "Jyothika", "marks": 90}
}
print(college["student2"]["marks"])
Explanation: Demonstrates a nested dictionary, where each key holds another dictionary.

10. Loop Through Dictionary


student = {"name": "Vishal", "age": 22, "course": "CSE"}
for key, value in [Link]():
print(key, ":", value)
Explanation: Iterates through a dictionary using the items() method to print keys and values.

You might also like