In this lab, you'll explore the fundamental building blocks of Python: variables, statements, and expressions. By the end, you'll have a solid understanding of how to create variables, write statements, and perform calculations using expressions.
- Understand the concept of variables and how to assign values to them.
- Learn to use basic arithmetic operators and write expressions.
- Recognize the difference between expressions and statements.
- Explore the
printfunction for displaying output. - Practice using the
importstatement to access modules and their functions.
- Create variables:
- Create a new file named lab02.py and assign the following values to variables within it:
name= "Your Name"age= 20 (or your actual age)height= 5.7 (or your actual height in feet)favorite_color= "Blue" (or your favorite color)
- Create a new file named lab02.py and assign the following values to variables within it:
- Printing Techniques for variable values:
- 1. Print one variable at a time:
- Print the value of each variable individually using separate
printstatements.
- Print the value of each variable individually using separate
- 2. Print with one
printstatement and commas:- Print the values of all variables in a single
printstatement, separating them with commas. - Does it add a space between the variables?
- Print the values of all variables in a single
- 3. Print with Python formats or format specifiers:
- Use Python's string formatting capabilities to print the variables in a more structured way.
- Here's an example with only 2 of your variables.:
print(f"Hello: {name} my is {favorite_color}!")
- Note the "f" at the beginning of the string tells Python to "format"
- Try different format specifiers to control the output (e.g.,
{:d}for integers,{:f}for floats).- Interact with your friendly AI chatbot of choice to get some examples of how to do this, and add statements to your code.
- 4. Print with format specifiers within a multi-line string:
- A multi-line string is a string that spans multiple lines. It is defined using triple quotes (
"""). - Use format specifiers within a multi-line string to create a formatted output.
- Here's an example with 2 of the variables you previously defined, you need to include them all:
print(f""" Name: {name} Age: {age} """)
- Test what happens if you add extra spaces at the beginning of the lines of multi-line strings?
- A multi-line string is a string that spans multiple lines. It is defined using triple quotes (
- 1. Print one variable at a time:
- Create a new variable:
- Similar to last week, calculate the area of a circle with a radius of 5, but this time store the result in a variable named
circle_area. - Print the value of
circle_areato ONLY 1 decimal place.
- Similar to last week, calculate the area of a circle with a radius of 5, but this time store the result in a variable named
- Import the
mathmodule:- Use the
importstatement to import themathmodule.
- Use the
- Calculate the square root:
- Use the
sqrtfunction from themathmodule to calculate the square root ofage. - Print the result.
- Use the
- Calculate the sine and cosine:
- Use the
sinandcosfunctions from themathmodule to calculate the sine and cosine ofheight. - Print the results.
- Use the
- Arithmetic operations:
- Use arithmetic operators (
+,-,*,/,//,%,**) to perform calculations. - Calculate the following:
- The sum of
ageand 5. - The difference between
heightand 4. - The product of
ageandheight. - The quotient of
heightand 2. - The remainder of
agedivided by 3. ageraised to the power of 2.
- The sum of
- Use arithmetic operators (
- Print the results:
- Use
printto display the results of each calculation. Choose your preferred print formatting from the previous exercise.
- Use
- Create a temperature conversion program:
- Write a program that converts Fahrenheit to Celsius.
- Prompt the user to enter a temperature in Fahrenheit.
- HINT: the
inputfunction will be necessary, and it takes the prompt string as the first argument.
- HINT: the
- Convert the temperature to Celsius using the formula:
Celsius = (Fahrenheit - 32) * 5/9. - Print the converted temperature and see if you can print the degree symbol.
- Create a GitHub Repository:
- Log in to your GitHub account.
- Create a new repository with a suitable name (e.g., "python_lab2").
- Configure Git in PyCharm:
- Open the "Git" menu in PyCharm and initialize a Git repository for your project.
- Add your GitHub remote repository by going to "Git" -> "Manage Remotes".
- Commit Changes:
- Stage all changes in your PyCharm project.
- Commit the changes with a descriptive message (e.g., "Initial commit").
- Push to GitHub:
- Push the committed changes to your GitHub repository using the "Push" button in PyCharm.
Submit the link to your GitHub repository containing the lab02.py file to the Canvas Lab!
- Remember to use proper indentation to organize your code.
- If you encounter errors, carefully read the error messages for clues.
- Experiment with different operators and functions to explore Python's capabilities.
- Use comments to explain your code and make it easier to understand.