A | Assignment |
1. Exercise
How do we store and manage data in a program?
Answer in your own words.
- Explain how data is stored and managed in a program.
- Why is it important to organize data instead of working only with raw values?
- Can variables change their value or data type?
2. Exercise
Define 4 variables with meaningful names and different data types.
Write your solution as a Python code.
Use this data types:
- A integer
- A string
- A float
- A boolean
Then, use print() to display each variable together with its data type using the type() function.
Example output format:
print("Price:", price, "| Type:", type(price))
3. Exercise
Mathematical operations and using the math
library
Write your solution as a Python code.
- Import the
math
module. - Define two integer variables.
- Use the following math functions:
pow()
,sqrt()
,log()
- Use
//
(floor division) and%
(modulo). - Store the results in new variables.
- Print each result together with its type using
type()
.
Make sure your variable names clearly describe what they store.
π Your print() statements should be well-formatted and readable β include clear labels for both the result and the data type.
Example:
print("Square root of x:", root_x, "| Type:", type(root_x))
4. Exercise
Boolean values and comparisons
Write your solution as a Python code.
- Define at least three boolean variables with meaningful names and print them.
- Define four numeric variables.
- Calculate the square root of each number.
- Compare the square roots using the greater-than operator (
>
). - Print whether one value is greater than another.
Bonus: Try using combinations of comparisons, like:
print(root_a > root_b and root_c > root_d)
5. Exercise
Strings and slicing
Write your solution as a Python code.
In this task, you will practice working with strings using only slicing ([:]
) β no other string functions or methods.
- Ask the user to input the following information using the
input()
function:- First name
- Last name
- Street address
- City
- Age (as string)
- Use slicing to:
- Extract the first 3 letters of the first name.
- Extract the last 2 letters of the last name.
- Extract the first 5 characters of the address.
- Extract the last 3 characters of the city.
- Extract only the first digit of the age string.
- Print all characters of the first name except the last one using
[:-1]
and explain what this slicing does. - Replace the first letter of the last name by concatenating slices (e.g., change βKleinβ to βBleinβ).
- Create a new string from the city where the middle part is removed using slicing.
- Use
print()
to show the results with labels. All outputs should be clearly formatted.
π Make sure to test your code with different inputs and see how slicing behaves.
6. Bonus Exercise
Converting Data Types in Python
Write your solution as a Python code.
You are developing a program for managing bakery orders. Simulate user input by defining all inputs as strings:
product_name = "Wholegrain Bread"
price = "3.79" # net price (without tax)
quantity = "12"
available = "True"
Your tasks:
- Convert:
- price β float
- quantity β int
- available β boolean
-
Print each converted value and its data type using
type()
.
Make sure the output is clearly formatted and easy to read. - Calculate total net price:
- Multiply quantity by net price
- Store and print result with its type
- Calculate total gross price (including 19% tax):
- Calculate 19% of the total net price
- Add it to the net total
- Store and print the gross price and its type
- Reverse conversion:
- Convert the total gross price back to a string
- Create a sentence like:
"12 Wholegrain Bread cost 45.48 euros (including tax)."
- Print the final sentence