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

Python Data Types and Programs Guide

Uploaded by

niaannaabraham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Python Data Types and Programs Guide

Uploaded by

niaannaabraham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ICT Python Worksheet – Answer Key

Section I: Data Types


Given code:

movieTitle = "The Jungle Book"


releaseYear = 2016
rating = 7.4
genre = "Adventure"

Variable Name Data Type Explanation

movieTitle String A string is used to store text,


such as the title of a movie.

releaseYear Integer The year is a whole number,


so it's stored as an integer.

rating Float Decimal numbers are stored


using the float data type.

genre String The genre is a word (text),


so it is stored as a string.

Section II: Student Info


Given code:

studentName = "Ravi"
rollNumber = 1024
height = 1.45
school = "Sunrise Public School"
isPresent = True

Variable Name Data Type Explanation

studentName String The name is text, so it is


stored as a string.

rollNumber Integer Roll numbers are whole


numbers.

height Float Height can have decimals,


so float is used.

school String The school name is a string


of text.

isPresent Boolean Boolean is used for True or


False values.

Python Programs

a. Convert Temperature from Celsius to Fahrenheit


celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit is:", fahrenheit)

b. Calculate Simple Interest


principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
simple_interest = (principal * rate * time) / 100
print("Simple Interest is:", simple_interest)

c. Calculate the Volume of a Cube


side = float(input("Enter the length of one side of the cube: "))
volume = side * side * side
print("Volume of the cube is:", volume)

d. Calculate BMI (Body Mass Index)


weight = float(input("Enter your weight in kg: "))
height = float(input("Enter your height in meters: "))
bmi = weight / (height * height)
print("Your BMI is:", bmi)

e. Temperature Check (Hot or Cold)


temp = float(input("Enter the temperature in Celsius: "))
if temp > 30:
print("It's hot today!")
else:
print("It's a cool day.")

f. Check if a Number is Positive, Negative or Zero


num = float(input("Enter a number: "))
if num > 0:
print("The number is positive")
elif num < 0:
print("The number is negative")
else:
print("The number is zero")

Error Detection and Correction


Code (with error) Issue Correct Code

item_price = input("Enter Input is string; cannot item_price =


the price of the item: ") multiply directly. String float(input("Enter the price
quantity = input("Enter the addition in print is of the item: "))
quantity: ") incorrect. quantity = int(input("Enter
total = item_price * quantity the quantity: "))
print("Total cost is: " + total = item_price * quantity
total) print("Total cost is:", total)

number = int(input("Enter a Used '=' instead of '=='. number = int(input("Enter a


number: ")) number: "))
if number % 2 = 0: if number % 2 == 0:
print("The number is print("The number is
even") even")
else: else:
print("The number is print("The number is
odd") odd")

pocket_money = Input is string, needs pocket_money =


input("Enter your monthly conversion. '+' in print float(input("Enter your
pocket money: ") needs to be replaced with monthly pocket money: "))
months = input("Enter comma or conversion. months = int(input("Enter
number of months: ") number of months: "))
total = pocket_money * total = pocket_money *
months months
print("Total pocket money print("Total pocket money
received is: " + total) received is:", total)

Common questions

Powered by AI

The error in calculating the total cost arose from treating input as strings, which cannot be directly multiplied. The issue was resolved by converting the item price to a float and the quantity to an integer before calculating the total cost, thereby ensuring the multiplication is valid. String addition in print was also corrected to use a comma or conversion method .

Converting input values to appropriate data types is necessary in Python to ensure that the values can be used correctly in arithmetic operations. For arithmetic, data types like integers or floats are required, as strings cannot be directly used in calculations. Conversions prevent errors and ensure data integrity by allowing proper computations .

Corrections made include converting pocket_money to a float and months to an integer to ensure valid arithmetic operations, as input defaults to strings. The print statement's concatenation method was also changed from '+' to comma, which is necessary to avoid type errors during string formatting. These corrections ensure data integrity and correct output display .

Using '==' instead of '=' is crucial in Python conditional statements because '==' tests equality while '=' assigns values. Misuse can lead to logic errors where conditions are replaced by unintended assignments, causing incorrect program behavior. Correcting this syntax ensures proper comparison and flow control, vital for accurate condition checking .

Temperature conditions in Python can be assessed using if-else statements. A condition like if temp > 30 checks whether the temperature exceeds 30 degrees Celsius, determining it's hot, while the else branch implies cooler temperatures. This logical branching allows the program to respond appropriately to varying temperature inputs .

Converting temperature from Celsius to Fahrenheit in Python demonstrates the use of multiplication and addition arithmetic operations. The conversion formula is fahrenheit = (celsius * 9/5) + 32 .

Boolean data types in Python provide True or False values, which are used in conditional logic and flow control. In checking student attendance, a boolean variable like isPresent holds a True value if the student is present and False otherwise. This binary state effectively represents basic conditions in attendance systems .

The method for calculating BMI in Python uses the formula bmi = weight / (height * height), where weight is in kilograms and height in meters. This formula reflects the ratio of weight to the square of height, providing a measure to assess body weight relative to height. The components involved are basic arithmetic operations and variables to hold inputs .

Logical conditions in Python use if-elif-else statements to check if a number is positive, negative, or zero. The condition num > 0 checks for positivity, num < 0 for negativity, and the else statement covers the zero case. These conditional checks control program flow based on the input value .

The appropriate data types for storing a student's name, roll number, and height in Python are: a string for name (text data), an integer for roll number (whole numbers), and a float for height (numbers with decimals).

You might also like