Introduction: This document presents learning steps for Python 02. In Python 02, you will learn basics of Python structures to add decision points to your programs. A branching program is a flow of sequential instructions with branching statements. By the end this week, you will be able to implement a program where a user can enter simple input, the program can make choices based on some conditions and after calculation, results can be printed.
Note: Exercises of this learning path can be done using:
- Online Python Editor OPyEditor: The final program should be stored on your local machine.
- Local Python Package (see Step-01): Using BRef-01: Appendix B Python can be installed on your local machine.
The activities are designed based on these following references:
- BRef-01: Book, Bill Lubanovic; "Introducing Python: Modern Computing in Simple Packages"; Available here
- OPyEditor: Online Editor for Programming; "Online Python (with shell and file storing functionalities)"; Available here
Follow these steps:
After taking this step, you will be able to:
1. implement and run your Python programs on your local machine.
- Using BRef-01: Appendix B perform the following tasks:
- Install Python on your machine.
- Open a terminal (command window) and check the version of your Python. Which command did you use?
- Using OPyEditor implement a program that prints a statement of a defined variable, like
Hello Python!
. Save the file on your local machine within a folder created by you. Using your terminal (command line) execute your first Python program. Which command do you need to execute a Python program?
- Create a file named
print_input.py
. Open it using an editor of your own choice. Enter your code in it to ask the user to input a text. Print that text. Run the file using the command line. - Read BRef-01: Chapter 01, Section Running Python and runPython shell. Excute
quit()
. What do you observe? - StartPython shell and execute
num = input('Enter a number:')
. Enter a number and print the value ofnum
. There are two different ways to print the value ofnum
. Try both at shell. Which one works in OPyEditor? Do you recognise differences between programming using python shell and an editor? Read BRef-01: Chapter 01, Section Running Python including subsections.
Note: After this step, you can try both python shell and editor to practice. It is recommended to use python shell for small experiments and use programming within editors (local or online) for writing a full program.
After taking this step, you will be able to:
1. interpret and implement boolean expressions.
2. implement Python programs with conditional statements.
- Using BRef-01: Chapter 04 discuss and experiment the following questions:
- What is a comment? How can you specify a comment in Python?
- What are: boolean values, boolean expressions, comparison operators?
- What is a conditional statement in Python? What is correct syntax for a correct if-else statament? What is a body of a if-else statement?
- Create a variable with the value
True
. Print it. Change the value toFalse
. Print it. - Create a variable
- named
a
with the valueTrue
, another - named
b
with the valueFalse
. Use print to check the output ofprint(a == b)
. - Do the same for the other comparison operators,
!=
,<
,>
,<=
,>=
.
- named
- Repeat the second exercise using an if-statement. Print
yes
orno
, for true false.- Write a comment above the if-statement explaining with it does.
- Implement a program in which the user is asked for input. Save the input of the user in a variable. Print
yes
if the input contains the charactere
,no
if not. - Think of an useful situation where you need to check something with a if-statement within another if-statement (nested if-statements). Code it and write a comment to explain why it needs a nested if.
- Finish all the exercises listed in BRef-01-Chapter 04: Things to Do.
After taking this step, you will be able to:
1. interpret and implement Python programs with Python functions: function definition, calling functions, return of a function, functions with arguments.
- Using BRef-01: Chapter 09 answer and experiment the following questions:
- What is a function in Python?
- What are the main elements of a Python function? Define a simple function that does nothing.
- How can a function be used (called)?
- How can one return the result of a function?
- What are the arguments and/or parameters?
Note: In the following exercises you can decide yourself what should be the name of function in your solution. Check PEP8 Function and Variable Names for guidelines.
- Explain in your own words the difference between
arguments
andparameters
. - Create a function that just prints the word
hello
. Call the function and run your program. Where the function is defined? Where is it called? - Create a function that takes a text as an argument. The function prints the text it receives. Call the function and run your program.
- Create a function that takes two numbers as argument. The function adds the numbers together and returns the results. Call the function and run your program.
- Create two functions, each takes a number as argument. The first one returns the number multiplied by 2 and returns it. The second multiplies it by 10 and returns it. Calling both functions add the two returned numbers together and print it. Run your program and check the results.
- Create two functions. One that prints
hello
, the other printsbye
. Ask the user to input a number, if the number is higher than 10, call the first function. If the number if lower or equal to 10, call the second function. Test your program. - Create two functions. One that prints
hello
, the other printsbye
. The first functions calls the second one after printing. Call the first function. - Provide your solutions to the exercises of Python 05: Step-01. ORef-01: Functions can be used as extra learning reference.
- Design two exercises of your own. They should improve understanding topics of this step.
- Install Visual Studio Code on your working machine. Implement and run a simple Python program of your choice.
- It is important to learn how to create a new Python program, how to configure interpreter and how to run the program. Where do you see the results?
-
Given the following problem statement, one of the students has submitted two solutions. The submitted solutions may not work correctly.
- Without executing the submitted code, check the implementation, analyze and find the issue(s).
- Use the following link to run the code step by step and visualize the execution. Python Execution Visualizer Note: Before pressing "Visualize Execution" you need to enter your input at "Enter optional text input for ...".
- This step-by-step execution should confirm the issues you have found in the code.
- After listing the issues, propose how the code must be fixed. Fix the code and again use step-by-step execution to see that the possible issues are fixed.
- Remember: the main goal is to learn how the program is executed step-by-step. Focus on the goal.
Problem Statement
Develop a program that reads a four-digit integer from the user and displays the sum of the digits in the number.
3141
3+1+4+1=9
Incorrect Solutions
Solution 1:fourdigit_num = int(input("Input a four digit number: ")) x = fourdigit_num // 1000 x1 = (fourdigit_num - x * 1000) // 100 x2 = (fourdigit_num - x * 1000 - x1 * 100) // 10 x3 = fourdigit_num - x * 1000 - x1 * 100 - x2 * 10 print("Sum:",x+x1+x2+x3)
Solution 2:
numstr = input("enter a 4 digit number") sum = 0 text = "" for i in range(len(numstr)): sum += int(numstr[i]) text = text + numstr[i] print(text)