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

Programming Assignment Unit 1

Uploaded by

Tsega Teklewold
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming Assignment Unit 1

Uploaded by

Tsega Teklewold
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Part 1

a) Quotation Marks Error

Code:

# Missing quotation mark

print("Tsega Teklemariam) # Error: SyntaxError

Explanation:

 If you leave out one of the quotation marks or both in Python, you will encounter a

`SyntaxError`. This is because Python interprets the text inside the quotes as a string.

Without proper quotation marks, Python cannot determine where the string begins or

ends.

Expected Output:

SyntaxError: EOL while scanning string literal

b) Difference between `*` and `**` Operators

Code:

# using * for multiplication

result_multiply = 3 * 4 =12

# using ** for exponentiation

result_exponentiate = 3 ** 2 =9

print(result_multiply, result_exponentiate)

Explanation:

 In Python, `*` is the multiplication operator, and `**` is the exponentiation operator. The

`*` operator multiplies two numbers, while the `**` operator raises one number to the

power of another.
c) Displaying an Integer like `09`

Code:

# trying to define an integer with a leading zero

num = 09 # Error: SyntaxError

Explanation:

 In Python, integers cannot have leading zeros (except for zero itself). If you try to define

a number like `09`, it will raise a `SyntaxError` because it's interpreted as an invalid octal

number.

Expected Output:

SyntaxError: invalid token

d) Difference Between `type('67')` and `type(67)`

Code:

type_string = type('67') # <class 'str'>

type_integer = type(67) # <class 'int'>

print(type_string, type_integer)

Explanation:

 The `type()` function in Python returns the type of an object. In this case, `type('67')`

returns `<class 'str'>` because it’s a string, while `type(67)` returns `<class 'int'>` because

it’s an integer.

Expected Output:

<class 'str'> <class 'int'>


Part 2 Ideas for Python Programs

i. Multiply you’re Age by 2

Code:

age = 32

result = age * 2

print(f"{age} * 2 = {result}")

Explanation:

 This program multiplies the user’s age by `2` and displays the result in a formatted string.

It demonstrates basic arithmetic and variable usage in Python.

ii. Display City, Country, and Continent

Code:

city = "Addis Ababa"

country = "Ethiopia"

continent = "Africa"

print(f"City: {city}, Country: {country}, Continent: {continent}")

Explanation:

 This program uses string variables to hold location information and displays it in a

structured format. It demonstrates how to concatenate and format strings in Python.

iii. Display Examination Schedule

Code:

start_date = "January 9, 2025”

end_date = "January 12, 2025"


print(f"Examination Schedule: {start_date} to {end_date}")

Explanation:

 The program stores the start and end dates of an examination schedule in variables and

displays them. This showcases the ability to store and manipulate date strings in Python.

iv. Display Current Temperature

Code:

temperature = 23°C

print(f"The current temperature in my country is {temperature}°C")

Explanation:

 This program allows for displaying the temperature, showcasing variable assignment and

formatted output in Python.

You might also like