Assignments Of Day 2 – Python Syntax and Variables
Assignments Of Day 2 – Python Syntax and Variables
Assignment
1: Install Python and Verify the Installation
Task: Install
Python and verify the installation on your system.
Steps:
1. Download
Python:
o
Visit https://www.python.org/.
o
Download the latest version suitable for your
operating system.
2. Install
Python:
o
Run the installer.
o
Check the box "Add Python to PATH".
o
Click on "Install Now" to complete
the installation.
3. Verify
Installation:
o
Open the terminal/command prompt.
o
Type:
python --version
o
The output will display the installed Python
version, e.g., Python 3.11.5.
Explanation:
This step ensures Python is installed and accessible from any directory on your
system.
Assignment
2: Write and Execute "Hello, World!"
Task: Write a
Python script to print "Hello, World!" on the screen.
Steps:
1. Open a
text editor (e.g., IDLE or Notepad).
2. Write the
following code:
print("Hello, World!")
3. Save the
file as hello.py.
4. Run the
program:
o
Using IDLE: Press F5 or click Run > Run
Module.
o
Using the terminal:
python hello.py
Output:
Hello, World!
Explanation:
The print() function outputs the text "Hello, World!" to the screen.
Assignment
3: Understanding Python as an Interpreted Language
Task: Use
Python's interactive mode to run commands line-by-line.
Steps:
1. Open the
Python shell by typing python in the terminal.
2. Type the
following commands one at a time:
print("Python is awesome!")
3 + 5
"Hello"
+ "World"
3. Observe
the results after each command.
Output:
Explanation:
Python executes commands immediately, showing results line-by-line, which is a
characteristic of interpreted languages.
Assignment
4: Create a Simple Calculator
Task: Write a
Python program to add two numbers.
Steps:
1. Write the
following code:
num1 = 10
num2 = 20
result =
num1 + num2
print("The
sum is:", result)
2. Save the
file as calculator.py.
3. Run the
program.
Output:
python
CopyEdit
The sum
is: 30
Explanation:
The program stores numbers in variables num1 and num2, calculates their sum,
and displays the result using print().
Assignment
5: Modify "Hello, World!"
Task: Print
your name along with "Hello, World!".
Steps:
1. Write the
code:
name = "John"
print("Hello,
World! My name is", name)
2. Save as hello_name.py.
3. Run the
program.
Output:
Hello,
World! My name is John
Explanation:
The variable name holds your name, which is included in the print() statement.
Assignment
6: Understanding Variables
Task: Assign
values to variables and print them.
Steps:
1. Write the
code:
x = 5
y = 10
z = x + y
print("Value
of x:", x)
print("Value
of y:", y)
print("Sum
of x and y:", z)
2. Save as variables.py.
3. Run the
program.
Output:
Value of x: 5
Value of
y: 10
Sum of x
and y: 15
Explanation:
Variables x and y store integers. The sum is calculated and displayed.
Assignment
7: Using Comments
Task: Add
comments to explain the "Hello, World!" program.
Steps:
1. Write the
code:
# This is a simple Python program
# It
prints "Hello, World!" to the screen
print("Hello,
World!")
2. Save as comments.py.
3. Run the
program.
Output:
Hello, World!
Explanation:
Comments start with # and are ignored by the Python interpreter.
Assignment
8: Using the Input Function
Task: Take a
user’s name as input and greet them.
Steps:
1. Write the
code:
name = input("Enter your name: ")
print("Hello,
" + name + "!")
2. Save as greet_user.py.
3. Run the
program and enter your name when prompted.
Example Input:
Enter your name: Alice
Output:
Hello, Alice!
Explanation:
The input() function takes user input as a string, which is concatenated with a
greeting.
Assignment
9: Experiment with String Operations
Task:
Concatenate two strings and print the result.
Steps:
1. Write the
code:
string1 = "Python "
string2 =
"Programming"
result =
string1 + string2
print("Concatenated
String:", result)
2. Save as string_operations.py.
3. Run the
program.
Output:
Concatenated String: Python Programming
Explanation:
The + operator concatenates string1 and string2.
Assignment
10: Understanding Error Messages
Task: Run a
program with an intentional error and analyze the error message.
Steps:
1. Write the
code with a missing closing quote:
print("This will cause an error)
2. Save as error_example.py.
3. Run the
program.
Output:
SyntaxError: EOL while scanning string literal
Explanation:
Summary
These assignments are designed to
introduce students to Python's fundamentals, including installation, syntax,
variables, and debugging. By completing them, students will gain confidence in
writing and executing Python programs.