100% found this document useful (1 vote)
11 views5 pages

Python Programming Basics and Errors

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
100% found this document useful (1 vote)
11 views5 pages

Python Programming Basics and Errors

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

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.
[Link] 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.
[Link] 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
[Link] 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. [Link]
Python Software Foundation. (n.d.). Using Python on Unix platforms.
[Link]
Python Software Foundation. (n.d.). Using Python on Windows.
[Link]
Savage, B. (n.d.). Using Python on a Mac. Python Documentation.
[Link]

Common questions

Powered by AI

Mastering syntax, data types, and operators is crucial in Python as it forms the foundation for all programming tasks. Syntax rules dictate how code must be written to be understood by the interpreter, while data types ensure appropriate handling and operations on data. Operators allow performing calculations and data manipulation. Proficiency in these areas enables accurate programming, reducing errors and enhancing the capability to leverage Python's full potential .

Learning about and experimenting with Python's error messages is crucial as it fosters a deeper understanding of the language's operational quirks and expected inputs. Error messages provide diagnostic feedback, pinpointing problems and offering learning opportunities to refine understanding of correct syntax and logical structures. This process builds problem-solving proficiency, essential for developers to independently troubleshoot and adapt code effectively .

The * operator in Python is used for multiplication, multiplying two numerical values. The ** operator, however, is used for exponentiation, raising a number to the power of another number. The choice between these operators depends on the specific mathematical operation required: use * for basic multiplication and ** for calculations involving powers and exponents, such as finding squares or cubes .

Python's type() function aids in managing data types by identifying an object's data type at runtime. This feature is particularly useful for preventing type-related errors by verifying that the expected data type aligns with operations being performed. For instance, ensuring that variables are integers before committing to mathematical operations can prevent runtime errors, promoting robust and error-free code execution .

Practicing syntax errors like missing quotes or incorrect integer formats helps reinforce understanding of Python's syntax rules by demonstrating how and why errors occur. This approach aligns with educational theories suggesting that learning from mistakes leads to deeper comprehension. It highlights the importance of syntax accuracy and encourages problem-solving skills as learners must deduce error sources and fixes .

In Python, '67' is treated as a string due to the surrounding quotes, while 67 is treated as an integer. This difference impacts their usage significantly; for example, arithmetic operations can be performed on integers, but attempting the same with strings results in errors unless explicitly converted. The type distinction also affects sorting, input/output handling, and data manipulation processes .

Python does not allow integer literals with leading zeros because starting with Python 3, a leading zero signifies an octal number, not a standard decimal integer. Allowing leading zeros in decimal integers could lead to misinterpretation of numbers. This requirement enforces clarity in number representation and prevents logical errors in code, ensuring that literals like '09' aren't misunderstood as octal values .

Using variables to display real-world data in Python involves declaring a variable to store specific information, such as a city's name or temperature, and then outputting that information using print statements. This process is significant as it makes programs dynamic, allowing them to handle actual data inputs and modify outputs based on these variables. For example, setting a temperature variable and using it to print a weather message helps understand how Python variables manage data contextually .

When a closing quotation mark is omitted in a Python string assignment, it causes a SyntaxError. This is because string literals in Python are expected to be enclosed in single or double quotes. Without the closing quotation mark, the interpreter looks for a variable with the name of the unclosed string, which likely does not exist, thus resulting in an error .

Python programming exercises improve skills in variable declaration by providing hands-on experiences in defining and using variables effectively within different contexts. Arithmetic practice in exercises enhances understanding of numerical operations and their correct implementation. Output formatting exercises optimize proficiency in arranging and presenting data cleanly and understandably, crucial for user interaction and data display in more complex applications .

You might also like