Understanding Python Fundamentals
Understanding Python Fundamentals
a. If you are trying to print your name, what happens if you leave out one of the quotation marks or
both, and why?
Code Example:
print("John) # Missing one quotation mark
print(John) # Missing both quotation marks
Explanation:
Missing one quotation mark results in a SyntaxError because the string is not properly closed.
Missing both quotation marks will cause a NameError as Python interprets John as a variable,
which is undefined.
# Exponentiation operator
result2 = 5 ** 3 # Expected output: 125
d. Run the commands type('67') and type(67). What is the difference in the output and why?
Code Example:
print(type('67')) # Output: <class 'str'>
print(type(67)) # Output: <class 'int'>
Explanation:
'67' is a string because it is enclosed in quotes, indicating a sequence of characters.
67 is an integer as it represents a numerical value without quotes.
b. Display the name of the city, country, and continent you are living in.
Code Example:
city = "New York"
country = "USA"
continent = "North America"
print("City:", city)
print("Country:", country)
print("Continent:", continent)
Explanation:
This program stores the city, country, and continent in variables and prints them in a readable format.
d. Display the temperature of your country on the day the assignment is attempted.
Code Example:
temperature = 18 # Celsius
print("Today's temperature:", temperature, "°C")
Explanation:
This program demonstrates how to display the temperature using a variable.
What I Learned
From these experiments, I gained a deeper understanding of Python's syntax and functionality.
Experimenting with errors clarified how Python identifies and reports issues like SyntaxError and
NameError. Exploring operators reinforced their functionality, particularly the distinction between
multiplication (*) and exponentiation (**). I also learned the significance of proper data types, as seen in
the difference between type('67') and type(67).
In Part 2, I applied fundamental Python concepts to practical problems. Writing programs with variables
helped me understand how data is stored and displayed. These exercises enhanced my debugging skills
and reinforced the importance of precise syntax and logical flow.
References
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf
Python Software Foundation. (n.d.). Python Documentation. Retrieved from
https://docs.python.org