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

Python Programming Basics LJ 22

Uploaded by

stardecos
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)
3 views5 pages

Python Programming Basics LJ 22

Uploaded by

stardecos
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

Unit 1: Programming Assignment: Introduction and Fundamental Concepts

BUS 3305-01 Business Law and Ethics - AY2025-T5


Python Programming Basics – Errors, Experiments, and Practical Applications
Part 1: Learn From Your Mistakes
Here I used my name Stella
print("Stella) # Missing end quote
print(Stella) # No quotes
If a closing quotation mark or both are omitted, a SyntaxError is thrown. This happens
because string literals are supposed to be inside single or double quotes. The lack of quotation
marks causes the interpreter to search for a variable named "Stella" which has not been
declared.

b. Difference between * and ** Operators


print(5 * 2) # Output: 10
print(5 ** 2) # Output: 2
The * operator multiplies two values, whereas the ** operator raises a number to a certain
power, i.e., exponentiation. For example, 5**2 raises 5 to the power of 2, or 25, and 5*2 just
multiplies 5 and 2.

c. Can Python Display Integer 09?


x = 09
print(x)
Python will throw "SyntaxError: leading zeros in decimal integer literals are not permitted."
This means that from Python 3 on, integers cannot start with a 0, unless the 0 is just meant to
be an octal indicator-e.g., 0o11. Hence 09 is just not valid syntax.
d.Difference Between type('67') and type(67)
print(type('67')) # <class 'str'>
print(type(67)) # <class 'int'>
67' is a string due to surrounding quotes; in contrast, 67 is an integer. This becomes relevant
when performing mathematical operations or text manipulation.

Part 2: Python Program Examples


a. Multiply Your Age by 2
age = 16
result = age * 2
print("Twice my age is:", result)
Explanation
The program defines age to be an integer and multiplies it by 2. This gives an idea about
variables and arithmetic operations in Python.
b. Display City, Country, and Continent
city = "Nairobi"
country = "Kenya"
continent = "Africa"
print("City:", city)
print("Country:", country)
print("Continent:", continent)
Explanation
Declaring string variables and printing them is an exercise in variable assignment and
string output.
c.Examination Schedule
start_day = "Monday"
end_day = "Friday"
print("Examination Schedule: From", start_day, "to", end_day)
The program outputs a message with the help of more than one string variable, thus
representing the building blocks of constructing formatted user messages
d.Display Today's Temperature
temperature = 25 # Celsius, assumed on date of attempt
print("Today's temperature is", temperature, "degrees Celsius.")
Explanation
It presents use of variables for real-world data like temperature
Summary
I learnt important programming concepts such as syntax, data types, and operators.
Trying out syntax errors in my code like forgetting to include quotation marks on a string
or formatting an integer incorrectly let me see how the python interpreter processes code
and flags problems. With the knowledge of the * and ** operators, I found more effective
ways to execute various mathematical operations (multiplying numbers/raising numbers
to the power of) quite useful.
I also learned that Python manages different data types through the type() function.
This is required for type-related error avoidance when dealing with real-life applications.
During the second part, short program writing improved my skills in declaring
variables, arithmetic, and aligning output. For instance, division of my age by 2
familiarized me with the use of integers in calculation, and the display of geographical
information showed how strings are formatted and printed understandably. Showing an
exam schedule and today's temperature also showed how Python can handle real-world
scenarios.
In general, I gained insights into introducing Python syntax and functions. It also
focused on awareness of mistakes as teaching aids, in agreement with Downey's (2015)
assertion that making errors and learning from them is the key to effective programming.
References
Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.).
O’Reilly Media. https://greenteapress.com/wp/think-python-2e/
Python Software Foundation. (n.d.). Using Python on Unix platforms.
https://docs.python.org/3/using/unix.html
Python Software Foundation. (n.d.). Using Python on Windows.
https://docs.python.org/3/using/windows.html
Savage, B. (n.d.). Using Python on a Mac. Python Documentation.
https://docs.python.org/3/using/mac.html

You might also like