Python Programming Lab
Experiment-1:install python and an IDE ,write and run a simple "Hello, World" program
1. Install Python
1. Go to the official Python website: Python Downloads.
2. Download the latest stable version of Python for your operating system (Windows,
macOS, or Linux).
3. Run the installer:
o For Windows, ensure that you check the box "Add Python to PATH" before
clicking "Install Now."
o For macOS or Linux, follow the installation instructions for your platform.
To verify the installation, open a terminal (or command prompt) and type:
bash
python --version
This should display the installed Python version.
2. Install an IDE (Integrated Development Environment)
You can use many IDEs, but I recommend VS Code or PyCharm for Python development.
Option 1: Install VS Code
1. Go to the Visual Studio Code website.
2. Download and install the version for your operating system.
3. Open VS Code, and install the Python extension by Microsoft.
o Go to the Extensions tab (left sidebar), search for "Python," and install it.
o VS Code will automatically detect your Python installation.
Option 2: Install PyCharm
1. Go to the JetBrains PyCharm website.
2. Download the Community Edition (free) for your OS.
3. Install and follow the setup instructions.
3. Write and Run a Simple "Hello, World" Program:Once you have Python and
your IDE installed:
In VS Code:Open VS Code, and create a new file by clicking File → New File.,Save the file
with a .py extension (e.g., hello.py).Write the following code in the file:
print("Hello, World!")
Right-click the file, and choose Run Python File in Terminal. You should see Hello,
World! printed in the terminal.
In PyCharm:
1. Open PyCharm, and create a new project.
2. Create a new Python file by right-clicking the project folder and selecting New →
Python File.
3. Write the same code:
print("Hello, World!")
4. Run the file by clicking the green play button or right-clicking the file and selecting
Run.
That's it! You have installed Python, set up an IDE, and run a simple Python program.
Experiment:2 create programs using variables,data types and operators
Example 1: Basic Variable Usage
# Declare variables
name = "Alice" # String data type
age = 25 # Integer data type
height = 5.7 # Float data type
is_student = True # Boolean data type
# Print variables
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is Student:", is_student)
Variables: We store information like a person's name, age, height, and student status.
Data Types: String (name), Integer (age), Float (height), Boolean (is_student).
Example 2: Using Arithmetic Operators
# Declare variables
num1 = 10
num2 = 3
# Perform arithmetic operations
addition = num1 + num2 # Addition
subtraction = num1 - num2 # Subtraction
multiplication = num1 * num2 # Multiplication
division = num1 / num2 # Division (float result)
floor_division = num1 // num2 # Floor division (integer result)
modulus = num1 % num2 # Modulus (remainder)
exponent = num1 ** num2 # Exponentiation
# Print the results
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)
print("Floor Division:", floor_division)
print("Modulus:", modulus)
print("Exponent:", exponent)
Arithmetic Operators: + (addition), - (subtraction), * (multiplication), / (division), //
(floor division), % (modulus), ** (exponentiation).
Example 3: Using Comparison and Logical Operators
# Declare variables
a = 15
b = 10
c = 15
# Comparison operators
print("a > b:", a > b) # Greater than
print("a == c:", a == c) # Equal to
print("a != b:", a != b) # Not equal to
print("b < c:", b < c) # Less than
# Logical operators
is_true = (a > b) and (a == c) # Both conditions need to be True
is_false = (a < b) or (a != c) # At least one condition needs to be True
not_true = not (a < b) # Negates the result
print("Both conditions True:", is_true)
print("At least one condition True:", is_false)
print("Negation:", not_true)
Comparison Operators: > (greater than), == (equal to), != (not equal to), < (less
than).
Logical Operators: and (both conditions must be true), or (at least one condition must
be true), not (negates the result).
Example 4: String Concatenation and Formatting
# Declare variables
first_name = "John"
last_name = "Doe"
age = 30
# String concatenation
full_name = first_name + " " + last_name
# String formatting using f-strings
greeting = f"Hello, my name is {full_name} and I am {age} years old."
# Print the results
print(greeting)
String Concatenation: Joining strings with the + operator.
String Formatting: Using f-strings to insert variables into a string.
Example 5: Using Assignment and Compound Operators
# Declare variable
x=5
# Assignment and compound operators
x += 3 # Equivalent to: x = x + 3
print("After addition:", x)
x *= 2 # Equivalent to: x = x * 2
print("After multiplication:", x)
x -= 4 # Equivalent to: x = x - 4
print("After subtraction:", x)
x /= 2 # Equivalent to: x = x / 2
print("After division:", x)
Compound Operators: += (add and assign), *= (multiply and assign), -= (subtract and
assign), /= (divide and assign).
Example 6: write programs using control structures
Example 1: if-else Control Structure
# Check if a number is positive, negative, or zero
number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
if-else:
This control structure allows the program to make decisions based on
conditions.
Example 2: for Loop
# Print all even numbers between 1 and 10
for i in range(1, 11):
if i % 2 == 0:
print(i)
forloop: Repeats a block of code a certain number of times. In this case, it iterates
from 1 to 10, checking if the number is even.
Example 3: while Loop
# Print numbers from 1 to 5 using a while loop
count = 1
while count <= 5:
print(count)
count += 1
whileloop: Repeats a block of code as long as a specified condition is true. Here, the
loop runs until count reaches 6.
Experiment 3:
Example 1: define and call functions with different types of arguments and return values
#(i) Function with No Arguments and No Return Value
# Define a function that prints a greeting message
def greet():
print("Hello, welcome to Python programming!")
# Call the function
greet()
#(ii)Function with Arguments
# Define a function that takes two numbers and prints their sum
def add_numbers(a, b):
sum = a + b
print(f"The sum of {a} and {b} is: {sum}")
# Call the function with arguments
add_numbers(5, 3)
#(iii)Function with Return Value
# Define a function that multiplies two numbers and returns the result
def multiply_numbers(x, y):
return x * y
# Call the function and store the return value
result = multiply_numbers(4, 5)
print(f"The result of multiplication is: {result}")