0% found this document useful (0 votes)
12 views

Lab 2 Manual - Programming For Business Lab

Uploaded by

yashfa ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab 2 Manual - Programming For Business Lab

Uploaded by

yashfa ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Your First Python Program:

Before you start this lab, you must have an environment set up on your computer to run a
python code. Run the following simple code which displays a message “Hello World!”
print("Hello World")

Understanding Variables:
Imagine you have a problem to solve that requires certain data, like keeping track of numbers.
Instead of writing the numbers down, in programming, you store them in variables—labeled
containers that hold the data you need to work with.

x = 10

Variables can have any name you choose, and they hold values that can be changed as
needed. Their name is what you use to access and work with the stored data.

You can apply operations on the variables and store result in a new variable

x = 10
y = 5
ans = x + y

You can store the following types of data based on your need
● Integer values (for example 5,10,100 etc)
● Decimal Values - also called float values (for example 1.2, 12.1 100.2 etc)
● Boolean Values (with value either being true or false)
● Character Value (for example ‘a’, ‘b’, ‘z’ etc)
● String Value (for example “abc”, “my name” etc)

Understanding Input/Output:
In the Python code above, the values of x and y are directly written into the code by the
developer (the person who writes the code). These are known as hardcoded values. Unlike
developers, end users interact with the finished software. If you want to receive input from end
users and display output back to them, you'll use the input and print functions in Python. Try
executing the following python code
x = int(input("Enter the first number: "))
y = int(input("Enter the second number: "))

ans = x + y
print("The sum is:", ans)

Following code is the example of input of different data types

# String input
name = input("Enter your name: ")
print("Your name is:", name)

# Integer input
age = int(input("Enter your age: "))
print("Your age is:", age)

# Float input
height = float(input("Enter your height in meters: "))
print("Your height is:", height)

# Boolean input (converting from string to boolean)


is_student = input("Are you a student? (yes/no): ").lower() == 'yes'
print("Student status:", is_student)

Understanding Arithmetic Operations:

In Python, you can perform basic arithmetic operations using the following symbols:

● Addition (+): Adds two numbers.


○ Example: x + y gives the sum of x and y.
● Subtraction (-): Subtracts the second number from the first.
○ Example: x - y gives the difference between x and y.
● Multiplication (*): Multiplies two numbers.
○ Example: x * y gives the product of x and y.
● Division (/): Divides the first number by the second, giving a float result.
○ Example: x / y gives the quotient of x divided by y.
● Modulus (%): Returns the remainder when the first number is divided by the second.
○ Example: x % y gives the remainder when x is divided by y.

Try the following python code.

x = 10

y = 3

print("Addition:", x + y) # Output: 13

print("Subtraction:", x - y) # Output: 7

print("Multiplication:", x * y) # Output: 30

print("Division:", x / y) # Output: 3.3333...

print("Modulus:", x % y) # Output: 1

Understanding Comments:
In Python, comments are used to explain the code and make it more readable. They are
especially useful for describing what specific parts of your code do, which can be helpful for
both you and others who may read your code later.

● Single-line Comments: Use the # symbol to add a comment on a single line.


Everything after the # on that line is ignored by Python.
○ Example: # This is a single-line comment
● Multi-line Comments: For longer explanations or comments that span multiple lines,
you can either use multiple # symbols or enclose the comment in triple quotes (""" or
'''). However, the latter is generally used for docstrings (documentation strings).

Lab Questions:
● Write a Python program that takes the obtained marks and total marks as input and
displays the percentage as output.
● Write a Python program that takes the principal amount, rate of interest, and time (in
years) as input, and calculates and displays the simple interest.
Formula: Simple Interest = (Principal * Rate * Time) / 100
● Write a Python program that takes the temperature in Celsius as input and converts it to
Fahrenheit.
● Write a Python program that takes the length and width of a rectangle as input and
calculates and displays its area.
● Imagine you have an infinite number of tables, each with a seating capacity of x people.
You have a total of n people to seat. The tables must be completely filled before starting
to fill another table. Write a Python program that takes the seating capacity x and the
total number of people n as input, and calculates how many people will be left without a
table after all the tables that can be completely filled are full.

You might also like