0% found this document useful (0 votes)
10 views5 pages

CS 1101-01 - Ay2025-T5 Unit 1

The document discusses common Python syntax errors and programming concepts, including issues with quotation marks, the use of operators, and integer literals. It also covers practical programming exercises, such as multiplying variables and displaying information, while emphasizing the importance of understanding data types and error handling. The reflection highlights the learning gained from experimenting with these coding practices and the significance of Python's design philosophy.

Uploaded by

Humayun Arain
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)
10 views5 pages

CS 1101-01 - Ay2025-T5 Unit 1

The document discusses common Python syntax errors and programming concepts, including issues with quotation marks, the use of operators, and integer literals. It also covers practical programming exercises, such as multiplying variables and displaying information, while emphasizing the importance of understanding data types and error handling. The reflection highlights the learning gained from experimenting with these coding practices and the significance of Python's design philosophy.

Uploaded by

Humayun Arain
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
You are on page 1/ 5

Part 1: Learn from Your Mistakes

Before beginning, I installed Python 3.10 on Windows following the official guide (Python
Software Foundation, n.d.-a). All code was executed in IDLE.

a. Missing Quotation Marks


# Code:
>>> print("Alice")
Alice
>>> print("Alice)
File "<stdin>", line 1
print("Alice)
^
SyntaxError: EOL while scanning string literal
>>> print(Alice)
File "<stdin>", line 1
print(Alice)
^
NameError: name 'Alice' is not defined

Output Explanation:

● With both quotes present, print("Alice") correctly outputs Alice.

● Omitting the closing quote causes a SyntaxError (EOL while scanning string
literal) because Python reaches the end-of-line without finding a matching delimiter.

● Omitting both quotes makes Python treat Alice as a variable name, leading to a
NameError since no variable Alice exists (Lutz, 2013).

b. * vs. ** Operators
# Code:
>>> 3 * 4
12
>>> 3 ** 4
81
Output Explanation:

● The single asterisk * performs multiplication: 3 * 4 yields 12.

● The double asterisk ** performs exponentiation: 3 ** 4 yields 34=813^4 = 81.


This distinction is fundamental in Python arithmetic and differs from some languages
that use ^ for exponent (Downey, 2015).

c. Leading Zero in Integer Literals


# Code:
>>> 09
File "<stdin>", line 1
09
^
SyntaxError: invalid token

Justification:
Python 3 disallows decimal literals with leading zeros to avoid confusion with octal notation. To
specify octal you must use the 0o prefix (e.g., 0o9 is invalid since 9 is out of octal range).
Therefore, you cannot write 09 (Python Software Foundation, 2021).

d. type('67') vs. type(67)


# Code:
>>> type('67')
<class 'str'>
>>> type(67)
<class 'int'>

Explanation:

● '67' is enclosed in quotes, so it is a string object (str).

● 67 without quotes is a numeric literal of type int.


This illustrates how Python distinguishes between text and numeric data based on literal
syntax (Downey, 2015).
Part 1 Reflection:
Experimenting with these errors reinforced my understanding of Python’s syntax rules and type
system. The quotation-mark exercise (1.9a) vividly showed how the interpreter parses string
delimiters and reports unexpected end-of-line. Distinguishing * and ** (1.9b) clarified operator
overloading in Python’s arithmetic model. Encountering the invalid leading-zero token (1.9c)
highlighted changes from legacy Python 2 (where 01 was octal) to Python 3’s stricter literal
rules. Finally, comparing type('67') with type(67) (1.9d) emphasized that literal syntax
determines object classes at parse time, impacting how values behave in expressions. These
hands-on mistakes are invaluable for internalizing error messages and deepening
comprehension of the language’s design philosophy.

Part 2: Writing Simple Python Programs

Note: All code was run on Python 3.10 IDLE (Python Software Foundation, n.d.-b).

a. Multiply Age by 2
# Code:
age = 22
result = age * 2
print("Twice my age is:", result)
# Output:
# Twice my age is: 44

What I Learned:
Multiplying an integer variable by 2 produces another integer. Assigning intermediate results to
variables improves readability and reuse. Printing with a comma automatically inserts a space.

b. Display Location Details


# Code:
city = "Karachi"
country = "Pakistan"
continent = "Asia"
print(city, country, continent)
# Output:
# Karachi Pakistan Asia
What I Learned:
String literals store textual data; the print() function can output multiple values separated by
spaces. Variables can hold metadata about the environment.

c. Examination Schedule
# Code:
start_date = "2025-07-01"
end_date = "2025-07-15"
print("Exam Schedule:", start_date, "to", end_date)
# Output:
# Exam Schedule: 2025-07-01 to 2025-07-15

What I Learned:
Dates can be handled as strings for simple display. In production, one would use the
datetime module for validation and formatting (e.g., datetime.date objects).

d. Display Today’s Temperature


# Code:
# Simulated: In a real script, you'd fetch from a weather API.
temperature_c = 36.5
print("Current temperature in", city, "is", temperature_c, "°C")
# Output:
# Current temperature in Karachi is 36.5 °C

What I Learned:
Retrieving real-time data typically requires HTTP requests to a weather API (e.g.,
OpenWeatherMap) and parsing JSON responses. Because the assignment did not mandate
internet connectivity, I simulated the temperature as a float. This exercise reinforced how
Python distinguishes numeric types: floats support decimals, ints do not. When working with
external data, one must handle potential exceptions—network timeouts, invalid JSON, or
missing fields—using try/except blocks. Additionally, formatting output for end-users often
involves concatenating strings and variables; f-strings (e.g., f"Current temperature in
{city} is {temperature_c} °C") provide a more concise syntax (PEP 498) and were
introduced in Python 3.6. Future enhancements could include converting between Celsius and
Fahrenheit, rounding to a fixed number of decimal places with the round() function, or
localizing units based on user preferences. Through coding these small programs, I gained
practical insight into Python’s data types, error handling needs, and string formatting
capabilities, which form the bedrock of more complex application development.
References

Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.). Green Tree
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf
Python Software Foundation. (n.d.-a). Using Python on Windows.
https://docs.python.org/3/using/windows.html
Python Software Foundation. (n.d.-b). Using Python on Unix platforms.
https://docs.python.org/3/using/unix.html
Python Software Foundation. (2021). What’s New in Python 3.10.
https://docs.python.org/3/whatsnew/3.10.html

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy