Part 2 - More Basics and Functions

This section will show you some different techniques that will come in handy when you are trying to make your output look consistent.

You will learn how to work with strings, and how to format and round decimal numbers.

Back to main

What to do/Hand In?

1 - Read through and do "Part 2 More Python Basics". Make sure you complete the Exercises.

2 - You will hand in Ch2.py as described in the reading. Make sure to put your name in a comment in at the top of your program, and label each part of the exercise with a comment.

3 - Look at the fString and Date.py program to help with formatted printing and dates.

If you want more information on what you can do with strings in Python, you can check out the official documentation here:

Datetime and Date

The datetime module includes functions and classes for doing date and time parsing, formatting, and arithmetic.

This website provides some easy to understand information regarding its use:

This link provides information on how you can get the month name and day name from a datetime object:

Submit:

  1. Ch2.py program as specified on Page 3 in Part 2: More Python Basics. Make sure to put your name in a comment at the top of your program!

    • There are 3 programming tasks listed there to do: Name (first and last) , Purchases with Tax and a separate Chatbot v1.0.

    • Make sure numbers are rounded and display an appropriate number of decimal points.

    • Provide detailed prompts when seeking input, and output.

Part 2 More Python Basics.pdf

Chatbot V1.0 Assignment

Part 2 - Chatbot v1.0.pdf

Hints and FAQ's

You will find that there are lots of different ways to do the same things in any programming language. Each way may have advantages or disadvantages in certain circumstances, but often it is a matter of preference.


Q: What is the difference between using an apostrophe (') and double quotes(")? (" vs. ') I see both in different tutorials!

A: They both mean the same thing either can be used. Pick one and stick with it. Don't mix them together.


Q: What is the difference between printing variables in the following examples? Why am I learning all of them?

Option 1:

print(f"Hello {name}, you are {age} years old!")

Option 2:

print("Hello", name, ", you are", age, "years old!")

Option 3:

print("Hello " + name + " you are " + str(age) + " years old!")

A: They are all different ways of doing pretty much the same thing. With Option 1 you have a bit more control of formatting, including determining spacing and rounding for formatted output of data stored in the variables. Option 2 and 3 are a bit more intuitive to use. Option 2 forces a space between each different part of the print statement. Option 3 requires you to put in spaces if you wish, and requires you to explicitly convert numeric (in this case an integer) variables to strings (because the '+' operator behaves differently when used on strings and numeric data types).

You are learning that all of them exist because as you seek out resources on the internet, you may encounter any of these methods, and you need to be aware that they exist. Use whichever you prefer.


Q: Do I have to explicitly convert numeric variable types into strings ( for example: str(number)) in order to use them in a print statement like I see in example code? According to some tutorials it seems necessary?

A: It depends on which method you are using to print.

For example:

number = 15


print("My favourite number is", number)

#In the above line, we do not need to convert to string because it is obvious what our intention is


print("My favourite number is " + str(number))

#In the above line, we do need to convert to string because it we are using the '+' operator. This operator means add if it has numeric values on either side, and it means concatenation if it has strings on either side, while its meaning is ambiguous if it is surrounded by both a numeric and non-numeric type.

5 + 5 = 10 #Computer knows to mathematically add them

"5" + "5" = "55" #Computer knows to concatenate (glue) them

5 + "5" = TypeError: unsupported operand type(s) for +: 'int' and 'str' #DANGER WILL ROBINSON