Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Programming Assignment 1

The document discusses various Python programming concepts: 1) It explains syntax errors that can occur when using quotation marks and discusses string and integer types. 2) It describes operators like *, **, / and their functions in Python. 3) Examples are provided to demonstrate printing values, using variables, and displaying output in formatted strings. 4) Dates, temperatures and other values are assigned and printed to show working with different data types in Python.

Uploaded by

Ridwaan Salie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Programming Assignment 1

The document discusses various Python programming concepts: 1) It explains syntax errors that can occur when using quotation marks and discusses string and integer types. 2) It describes operators like *, **, / and their functions in Python. 3) Examples are provided to demonstrate printing values, using variables, and displaying output in formatted strings. 4) Dates, temperatures and other values are assigned and printed to show working with different data types in Python.

Uploaded by

Ridwaan Salie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PART 1:

A. When I leave out one or both of the quotation marks around my name, it will
result in a syntax error. In Chapter 1. The way of the program, Pp 1.3 informs
us that ' The quotation marks in the program mark the beginning and end of
the text to be displayed, they don’t appear in the results.’ ( Downey, A. (2015).
Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf
Chapter 1 The Way of the Program, Pp 1.3)

>>> print(Ridwaan Salie)


File "<stdin>", line 1
print(Ridwaan Salie)
^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

1(b) >>> print("Ridwan Salie)


File "<stdin>", line 1
print("Ridwan Salie)
^
SyntaxError: unterminated string literal (detected at line 1)
>>>

B. The following * is an Operator that is used for multiplication. The ** is also an


Operator but it is used for exponentiation.

Chapter 1. The Way of Program, Pp 14 Arithmetic Operators provide the following.


"The operators +, -, and * perform addition, subtraction, and multiplication, as in the fol-
lowing examples:
>>> 40 + 2
42
>>> 43 - 1
42
>>> 6 * 7
42
The operator / performs division:
>>> 84 / 2
42.0
You might wonder why the result is 42.0 instead of 42. I’ll explain in the next section.
Finally, the operator ** performs exponentiation; that is, it raises a number to a power:
>>> 6**2 + 6
42" ( Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree
Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf, Chapter 1, The Way of the
Program)

Chat gpt explains the following “Operators are for unpacking elements from iterable
objects and for accepting variable numbers of arguments in function definitions,
respectively’’

The * operator is often referred to as the "splat" or "star" operator

The ** operator is used for unpacking keyword arguments in function calls and for
creating dictionaries by unpacking key-value pairs.
It allows you to pass a dictionary of keyword arguments to a function or to merge
dictionaries.” (Chatgpt,
https://chat.openai.com/c/b37ac0a0-072b-4950-8870-6c156cac050f, Downey, A.
(2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf, Chapter 1, The Way of the
Program)

C. You cannot display an integer with a leading zero like "09" directly. If you try to do
so, you will encounter a syntax error. This is because a leading zero in an integer
literal signifies an octal (base-8) number in Python. ( ChatGPT,
https://chat.openai.com/c/b37ac0a0-072b-4950-8870-6c156cac050f)

The following syntax is received when an integer 09 is displayed,


type(09)
^
SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o
prefix for octal integers
>>>
D. type('67') returns the following syntax:
<class 'str'>.

type(67) returns the following syntax:


<class 'int'>

The difference between the two is that

● type('67') returns <class 'str'> because '67' is a string.


● type(67) returns <class 'int'> because 67 is an integer.

The quotes around '67' make it a string, while the absence of quotes around 67 makes
it an integer. Python recognizes and treats these types differently.

Chapter 1. The way of the program, Pp 1.5 Values and types explains the different types.

“A value is one of the basic things a program works with, like a letter or a number. Some
values we have seen so far are 2, 42.0, and 'Hello, World!'.
These values belong to different types: 2 is an integer, 42.0 is a floating-point number, and
'Hello, World!' is a string, so-called because the letters it contains are strung together.
If you are not sure what type a value has, the interpreter can tell you:
>>> type(2)
<class 'int'>
>>> type(42.0)
<class 'float'>
>>> type('Hello, World!')
<class 'str'>
In these results, the word “class” is used in the sense of a category; a type is a category of
values." ( Downey, A. (2015). Think Python: How to think like a computer scientist. Green
Tree Press. https://greenteapress.com/thinkpython2/thinkpython2.pdf, Chapter 1, The Way of
the Program)
Part 2:

A. age = 20
results = age * 2
print(results)
40

What I have learned is that when you use an input by a print statement you can get the output
you are looking for. Age is assigned the integer 20, The function then uses the print()
function to output the values of these variables in a formatted format .

The * Operator was also used to multiply the integer by 2 which is the age.

B. city = "Johannesburg"
>>> country = "South Africa"
>>> continent = "Africa"
>>> print(f"I am living in {city}, {country}, {continent}.")
I am living in Johannesburg, South Africa, Africa.

The City, Country, and Continent were assigned the string "Johannesburg", "South Africa",
"Africa". The function then uses the print() function to output the values of these three
variables in a formatted string. The curly braces {} are placeholders for the values that will be
passed through the .format() method.

C. start_date = "November 2, 2023"


end_date = "November 5, 2023"
print("Exam schedule:", start_date, end_date)
Exam schedule: 2 November 2023 5 November 2023

I learned to use the start and end date as inputs and displayed an exam schedule. November
is used as a string
D. temperature = 29
print("temperature in Johannesburg the day I attempted the assignment:",
temperature, "degrees Celsius")
temperature in Johannesburg the day I attempted the assignment: 29 degrees Celsius

I took the temperature and then displayed the temperature for my city.

References:

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
https://greenteapress.com/thinkpython2/thinkpython2.pdf (Chapter 1)

ChatGBT (https://chat.openai.com/c/b37ac0a0-072b-4950-8870-6c156cac050f)

You might also like