0% found this document useful (0 votes)
8 views2 pages

Python Basics and Exercises Guide

Uploaded by

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

Python Basics and Exercises Guide

Uploaded by

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

# Print

print("Hello World!")
print("What is your name?")

# input () will get user input in console


input("What is your name?")

# save the input as a variable


# Then print() will print the word "Hello" and the variable
name = (input("What is your name?"))
print("Hello " + name)

# Coding exercise - ice cream name generator


# you are a food scientist and you are trying to come up with a name for your
new ice cream flavor. Your ice cream name will be: your favorite color and
the fruit that you hate.
# For example, if your favorite color is blue and the fruit that you hate is
strawberry, your ice cream name will be Blue Strawberry

color = (input("What is your favorite color?"))


fruit = (input("What fruit you hate the most?"))
print("Your new ice cream flavor is " + color + " " + fruit)

#Data Types
#"String" - alwasy with double quotes

print("Hello")

#Integer - whole numbers, without " " python will treat numbers as numbers
and not string
print(123 + 123)
print(123_456_789)

#Float - numbers with decimal places, floating point number


# 3.14159

#Mathematical Operations in Python


# 3 + 5
# 7 - 4
# 3 * 2
# 6 / 3
# 2 ** 3
# PEMDASLR - order of priority
print(3*(3+3)/3-3)
print(3**2)

# Rounding
print(round(8/3, 2)) #round to 2 decimal places

#Change data type from integer to float


a = float(123)
print(type(a))
# Change data type from string to float
print(70 + float("100.5"))

#f-String - mix strings and different data types


age = 30 #integer
height = 1.8 #float

#Use f-String to convert all data types to string


f"your age is {age} years old, your height is {height} meters."
print(f"your age is {age} years old, your height is {height} meters.")

# Coding exercise - discount calculator


#If the total bill was $150.00 with 15% discount, the final amount you have
to pay is ($150*(1-0.15)) = $120
#Round the result to 2 decimal places.
print("Welcome to the discount calculator!")
bill = float(input("What was the total bill? $"))
discount = int(input("How much is the discount in percentage? "))

discount_percentage = discount / 100


discount_amount = bill * discount_percentage
total_payment = bill - discount_amount
final_amount = round(total_payment, 2)

print(f"The final amount to pay after a {discount}% discount is:


${final_amount}")

Common questions

Powered by AI

To convert a string to an integer, Python uses the int() function, while conversion to a float uses the float() function. For instance, converting a string to a float can be performed by float('100.5'), resulting in 100.5. Similarly, converting a number into a float can be done using a = float(123), turning it into 123.0 .

Python uses the input() function to receive user input from the console and the print() function to output information. For example, when asking for a user's name, input("What is your name?") captures the input, and then print("Hello " + name) outputs the greeting by concatenating the string 'Hello' with the stored input variable 'name' .

Data types are crucial in Python for defining how data is stored and manipulated. Strings are enclosed in double quotes, such as "Hello". Integers are whole numbers without quotes, for example, 123. Floats are decimal numbers, like 3.14159. Operations vary depending on data type and include concatenation for strings and arithmetic for numbers, such as addition (3 + 5).

To calculate the final payment after a discount in Python, first, convert the bill and discount inputs into numbers. Next, compute the discount percentage by dividing the discount by 100. Then, calculate the discount amount by multiplying the bill by the discount percentage. Subtract the discount from the bill to get the total payment. Finally, use round() to round this result to two decimal places for accurate financial representation, which is critical for clarity and precision in financial transactions .

Python performs rounding operations using the round() function, which rounds a number to a specified number of decimal places. This is critical in financial contexts to ensure precision in monetary values, preventing errors in transactions. For example, rounding 8/3 to 2 decimal places results in 2.67, which ensures financial reports and calculations are accurate and consistent .

In Python, the order of mathematical operations is determined by PEMDASLR, which stands for Parentheses, Exponents, Multiplication and Division (from left to right), and Addition and Subtraction (from left to right). This order dictates how expressions are evaluated and ensures accurate calculations. For example, the expression 3*(3+3)/3-3 evaluates to 3 because operations within parentheses are calculated first, followed by division, and then subtraction .

Challenges in working with user input and data conversion in Python include handling invalid data types and ensuring data integrity. Solutions involve validating inputs, using try-except blocks to catch errors, and providing user prompts or defaults for incorrect inputs. These methods prevent runtime errors and enhance program robustness by ensuring that inputs are correctly formatted and converted, as demonstrated in leveraging input and float() for calculations .

Using variables like 'discount_percentage' and 'final_amount' in calculations enhances code clarity, maintainability, and complexity management. They help in breaking down a problem into smaller, manageable parts, allowing for easy updates and changes. Specifically, 'discount_percentage' makes the discount calculation transparent and 'final_amount' provides a final, rounded result for display after computing intermediate values .

Python's f-string allows for the inclusion of variables within strings seamlessly by using curly braces. This improves readability and flexibility as it automatically converts other data types to strings. For example, f"your age is {age} years old, your height is {height} meters." includes both an integer and a float within a single string output .

Creating a custom string operation, like an ice cream name generator, involves concatenating user input to form a new string. This demonstrates string manipulation and user interaction in Python. For instance, combining 'color = input("What is your favorite color?")' and 'fruit = input("What fruit you hate the most?")' into 'Your new ice cream flavor is ' + color + ' ' + fruit' teaches how variables can be used to create dynamic and user-customized outputs .

You might also like