Introduction: This document presents learning steps for Python 03. In Python 03, you will learn the basics of strings in Python and required programming structures to add repetition points to your programs. An iterative program is a flow of sequential instructions with repeating statements. By the end of Python 03, you will be able to implement a program where a user can have simple interactions: the user repeatedly enters simple input, the program calculats and prints the results.
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: Using BRef-01: Appendix B Python can be installed on your local machine.
The learning steps 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. interpret and implement Python programs using string operations and functions: *, +, len(), split(), join(), replace(), subsrtings, slicing, f-strings.
- Using BRef-01: Chapter 05 experiment and answer the following questions:
- What is string data type?
- Which Python built-in function can be used to convert a data type to string? Try some examples in Python shell.
- In Python (and some other programming languages), special characters can be included in a string. How one can specify these special characters in a Python program? List some of these characters and try the examples in Python shell.
- How one can concatenate multiple strings? Make one string with your: First name, Last name, student number and group number.
- What would be the result of multiplying a number with a string? Try three examples. What if the number is zero?
- How can you extract specific character from a given string? How can you specify the first character? Last character? How can you get a slice if a string? Try with several examples in Python shell.
- What are the functionality of functions: len(), split(), join(), replace(). Try two examples for each function in Python shell.
- Ask the user to input a text. Print the length of the entered text.
- Ask the user to input a text. Replace the first character with a
k
and print the result. - Make a variable with the text "this is a text". Remove all spaces from it. Print the result.
- Ask the user to input a text. Capitalize the complete input. Print the result.
- Ask the user to input a text. Remove all
e
characters from it. Print the result. Try to see what happens if the input doesn't contain ane
. - Ask the user to input a text. Count how many times the input contains the character
i
. Print the result. - Ask the user to input two texts (two inputs). Print them together in one line using a
f string
.
After taking this step, you will be able to:
1. interpret and implement Python programs using while loops.
- Using BRef-01: Chapter 06 answer and experiment the following questions:
- A while loop consists of a condition and a body. Pick and example from BRef-01: Chapter 06 and specify the condition and the body of the program.
- Using while loop implement a program that prints a message (like Hello) for 10 times.
- Using while loop implement a counter that counts down from 10 until 0. In each iteration, the program must print the value of the counter.
- What is break statement?
- Implement a program that repeatedly asks the user to enter a character as input until the user enters q. If the user enters q the program will stop.
- Print the numbers 1 to 42 using a
while
loop. - Print all odd numbers between 1 to 100 by using a
while
loop. - Print the numbers from 10 to -10 using a
while
loop. - Ask the user to input a text. Print each character of the input on a new line using a
while
loop. - Ask the user to input a text. Print each character of the input that is the character
e
ora
on a new line. - What will be the output of the given code?
i = 20
while i < 42:
i = i * 2
print(i - 1)
- What will be the output of the given code?
i = -4
end = -33
while i > end:
i = i - 4
print(i * 2)
- If we swap the last two lines in the previous exercise we get a different output. Why is this?
i = -4
end = -33
while i > end:
print(i * 2)
i = i -4
After taking this step, you will be able to:
1. interpret and implement Python programs using for loops.
- Using BRef-01: Chapter 06 answer and experiment the following questions:
- What are the main elements of a for loop?
- Using for loop implement a program that prints a message (like Hello) for 10 times.
- Using for loop implement a counter that counts down from 10 until 0. In each iteration, the program must print the value of the counter.
- Can you use a break statement within a for loop? Build a simple example.
- Implement a for loop that prints characters of a given string.
- Implement a for loop that given a string, prints characters positioned in odd indeces, i.e.
1,3,5,7,...
.
-
Print the numbers 1 to 42 using a
for
loop. -
Print all uneven numbers between 1 to 100 by using a
for
loop. -
Ask the user to input a text. Print each character of the input on a new line using a
for
loop. -
The
for
andwhile
are considered 'loops'. Explain in your own words what a loop is. -
Describe the difference between the
for
andwhile
in your own words. -
Practice the exercises listed in BRef-01-Chapter 06: Things to Do:
- 6.1, 6.2 and 6.3.
- For each of the following given codes:
- Without executing the code try to read the code and write down what will be the output.
- Use the Python Code Visualizer and execute the code step-by-step. Observe how the variables and statements are executing in each iteration of the loops.
# Code 1
i = 7
for number in range(1, i + i):
print(number)
# Code 2
i = 1
j = 10
for number in range(i, j):
if number > 5:
print(number)
else:
print('Hello')
# Code 3
sentence = "I just came to say hello!"
count = 0
for letter in sentence:
if letter == " ":
count = count + 1
elif letter == "a":
count = count - 1
print(count)
# Code 4
sentence = "I just came to say hello!"
for i in range(0, len(sentence)):
print(sentence[i])
# Code 5
sentence = "I just came to say hello!"
for c in sentence:
print(c)
Data formats are essential in the world of computing, providing a standardized way to represent and manipulate information. One of the most fundamental data formats is binary, which uses a base-2 numbering system composed of only two digits: 0 and 1. Binary is the foundation of all digital systems, with each digit representing the state of an electronic switch. Hexadecimal (hex) is another commonly used data format, employing a base-16 numbering system. It extends beyond the limited set of digits in decimal, introducing the additional symbols A-F to 0-9. Hexadecimal is frequently used to represent and display binary data in a more compact and human-readable format. On the other hand, decimal is a base-10 numbering system that most people are familiar with, utilizing ten digits 0-9. Decimal is widely used in everyday life and is the standard format for representing numerical values. Understanding these data formats is crucial for effectively working with data in various computing applications and systems.
-
Perform a free (re-)search and explore the answers for the following questions:
- Digits in decimal numbers are 0-9. What are the digits in hexadecimal format? What are the digits in binary format?
- Convert (manually) the following decimal numbers to hexadecimal and binary: 8, 10, 15, 21, 32, 64, 256, 500, 512, 1000.
- How does Python represent these data formats? How can you use Python to convert these data formats to each other?
-
Use Python to:
- Convert the decimal number 45 into its binary representation.
- Convert the binary number 1010101 into decimal form.
- Add the binary numbers 10111 and 1101 and express the result in binary.
- Convert the decimal number 255 into its hexadecimal representation.
- Convert the hexadecimal number 2A into decimal form.
- Add the hexadecimal numbers C4 and 3A and express the result in hexadecimal.
- Convert the binary number 1101 into decimal form.
- Convert the hexadecimal number F0 into decimal form.
- Add the decimal numbers 123 and 456.
- Convert the decimal number 157 into binary and then into hexadecimal.
- Convert the binary number 11101101 into decimal and then into hexadecimal.
- Convert the hexadecimal number AB4 into decimal and then into binary.
-
Real-life Applications:
- Research and identify a real-world example where binary data is used extensively.
- Investigate how hexadecimal is used in computer memory addressing.
- Explore how decimal data formats are used in financial calculations or accounting systems.