diff --git a/02-TypesAndVariables/02-TypesAndVariables.md b/02-TypesAndVariables/02-TypesAndVariables.md new file mode 100644 index 0000000..f87d427 --- /dev/null +++ b/02-TypesAndVariables/02-TypesAndVariables.md @@ -0,0 +1,430 @@ + + +# TYPES AND VARIABLES + +## Interactive and Script Modes + +1. In Python programming language, there are two ways in which you can run your code: + + * Interactive mode + * Script mode + + Interactive mode is a feature that allows you to execute commands one at a time and see the output immediately. It is often used for testing small pieces of code, debugging, or learning the language. To run Python in interactive mode: + + 1. Open your command line (Terminal window). + 1. Type 'py', 'python' or 'python3' (depending on your setup) and press Enter. + 1. You will enter the Python interactive shell where you can type and run Python commands interactively. The prompt usually looks like this:\ + \>\>\> + +1. In interactive mode, execute the following commands (enter a command and press Enter): + + 1. 7 + 4 + 1. (30 + 10) / 2 + 1. 2 ** 3 + 1. 7 > 5 + 1. r = 4 + 1. area = 3.14159 * r * r + 1. area + 1. round(area,2) + 1. first_name = "Mary" + 1. last_name = "Brown" + 1. full_name = first_name + ' ' + last_name + 1. full_name + 1. print(full_name) + 1. len(full_name) + 1. Enter commands to calculate and display the area of ​​a triangle with dimensions a and h. + 1. Enter commands to calculate and display the arithmetic mean of the numbers: 7, 12 and 31. Display the result with an accuracy of 2 decimal places. + +1. Script mode is used to write and execute program code from a file, instead of typing commands interactively. To write and run your program in script mode: + + 1. Use any text editor to write your program code. + 1. Save the file with a .py extension (for example: myprogram.py). + 1. Open the terminal window in a folder where you saved your program file. + 1. Run the script by typing: + + ``` + py myprogram.py + ``` + + Below is a program to calculate Body Mass Index. Run the program in Script mode. + + ```python + ### + # BMI Calculator + # + height = input('Enter your height in cm: ') + weight = input('Enter your weight in kg: ') + height = int(height) + weight = int(weight) + bmi = weight / (height/100)**2 + bmi = round(bmi,2) + print('Your BMI is', bmi) + ``` + +1. Explain the concepts + * data type + * variable + * operator + +1. Check what data types and operators are available in Python. + +1. From the chapter 2, complete all exercises in the Exercises section + + 1. Use VSCode to create and run programs + 1. In VSCode, open the folder 02-TypesAndVariables available in your local repository + 1. Save every created program in that folder + 1. After completing all exercises, save changes to your local repository and then to your remote repository on Github. You can use the following Git commands: + + ``` + git add . + git commit -m "My first Python programs from textbook" + git push + ``` + + + + Check if your remote Github repository contains all the programs you created. + +## Types, operators and expressions + +1. Consider what data type the following values represent. Then check your answers (run Python in interactive mode). You can use the available type(value) function. + + 1. 50 + 1. 149.17 + 1. 4 * 7 + 1. 4.0 * 7 + 1. "Krakow University of Economics" + 1. True + 1. 2 > 5 + +1. Count how many operators and arguments are included in each expression. Then, calculate values of the expressions. First, try to calculate every expression without using a computer. Then, check results on your computer (run Python in interactive mode). + + 1. 3 * 2 + 1 + 1. 5 + 10 * 5 + 1. 4 + 4 / 2 ** 2 + 1. 4 % 3 % 2 % 1 + 1. 1 + 2 % 3 ** 4 * 5 + 1. True != False + +## Variables + +1. Below is a program that calculates the sum of two numbers. Modify the program to calculate the sum of three numbers. + + ```python + # A program that calculates the sum of two numbers. + # Modify the program to calculate the sum of three numbers. + number1 = 71 + number2 = 14 + result = number1 + number2 + print('Number 1: ', number1) + print('Number 2: ', number2) + print('The result of summation: ', result) + ``` + +1. Natural values 15, 1, 8, 6, 31 have been assigned to variables named n1, n2, n3, n4, n5. Write a program that calculates and displays: + + 1. Sum of all variables + 1. Sum of squared variables + 1. Quotient of the variable three and five + 1. Message (True / False) indicating if the third variable is equal to the fourth variable + +1. One of the basic functionalities of a computer program is displaying results on the computer screen. To display the results in a readable form, string (text) formatting, available in programming languages, is often used. In Python, it is called f-strings. + + https://www.pythontutorial.net/python-basics/python-f-strings/ + + https://docs.python.org/3/tutorial/inputoutput.html + + Here is an example of displaying any text along with the value of a variable using f-strings: + + ```python + name = "Adam" + print(f"I am {name}, a student of this university") + ``` + + Let three variables: name, age and height contain your personal data. Write a program that, using f-strings, displays the following sentence: + + ``` + My name is ..., I am ... years old, and my height is ... cm. In 6 years I will be ... years old. + ``` + +1. Write a program that calculates and displays the income of a family per person. To display the results in a readable form, use f-strings. Sample result: + + ```python + father_income = 5450 + mother_income = 4920 + number_of_people = 5 + Total income: + Income per person: + ``` + +## Data Input + +1. Write a program that reads your name and surname from the keyboard. Store this data in two separate variables. Then, display your first and last name separated by a single space. Sample result: + + ```python + # Write a program that reads your first and last name and from the keyboard. + # Store this data in two separate variables. + # Then, display your first and last name separated by a single space. + first_name = input('Enter your first name: ') + last_name = input('Enter your last name: ') + full_name = first_name + ' ' + last_name + print(f'Your first name is {first_name} and your last name is {last_name}') + print(f'Your fullname is {full_name}') + ``` + +1. Write a program that calculates the surface area of a cube. Read the length of the side of the cube from the keyboard. Take into account that the input() function returns a string, not a number. To convert a string to a number, use the int() function. Sample result: + + ``` + Enter cube side: 6 + The surface area of a cube with side 6 is 216 + ``` + +## Algorithms + +1. The radius of the circle has the value 5, stored in a variable. Write a program that calculates the area and circumference of the circle. Use the algorithm below. + + ```python + ##### + # Calculation of circle area and circumference + ## + + # determine radius and PI + # calculate area + # calculate circumference + # display results + ``` + +1. Write a program that reads temperature in degrees Celsius from the keyboard. Then, the program calculates and displays the temperature in Kelvin and Fahrenheit. Use comments to briefly describe the program's algorithm. + +1. Evaluate the following expressions. First, try to do it without using a computer. Then, check the results on your computer (run Python in interactive mode). + + * The product of the numbers 15 and 38 + * The product of the sum of pairs of numbers 3 and 4, and 5 and 9 + * Integer part of dividing the numbers 7 and 2 + * The remainder of the division of 48 and 5 + * Arithmetic mean of the numbers 8, 7, 4, 2 + * 210 + * Square root of 49 (do not use a function) + * 25% of 80 + +1. Calculate values of the following expressions. First, try to do it without using a computer. Then, check the results on your computer (run Python in interactive mode). + + * 5 + 10 * 5 + * 3 � 2 + 1 + * 2 + - 3 + * 2 ** 8 + * 4 + 4 / 2 ** 2 + * 4 % 3 % 2 % 1 + * 1 + 2 % 3 ** 4 * 5 + * True != False + * 2 <= 3 or False + * not True or not False and not True + * 2 < 3 and 4 < 5 or not 6 < 7 + * 2 % 3 < 4 / 5 and 6 + 7 < 8 or not 9 + 10 == 19 + * 0x11 + 0b11 + 11 + +1. A variable x has a value of 7 and a variable y has a value of 34. Write a program that swaps variable values (x should be 34 and y should be 7). You can use one additional, auxiliary variable. Sample result: + + ``` + Value of x: 7 + Value of y: 34 + Value of x after swapping: 34 + Value of y after swapping: 7 + ``` + +1. The length of the side of the cube is contained in the variable a. Write a program that calculates and displays the volume and surface area of the cube. Sample result: + + ``` + Cube side: 4 + Cube volume: 64 + Cube surface area: 96 + ``` + +1. Variables a and b contain two integer numbers. Write a program that calculates and displays the result of their division, rounded down to the nearest whole number. Display also the remainder of the division. Sample result: + + ``` + Number one: 17 + Number two: 5 + Division result: 3 + Remainder: 2 + ``` + +1. A variable contains your height in cm. Write a program that displays your height both in cm and in feet and inches. Sample result: + + ``` + I am 170cm tall, i.e. 5 feet and 7 inches + ``` + +1. The variables a and b contain two integers, 3 and 5, respectively. Write a program that displays the following expression containing the values of these variables and the value of the expression. + + ``` + 3 � 5 = -2 + ``` + +1. The length of the sides of the triangle is a, b and c. Write a program that calculates the area of the triangle (using the Heron formula) and the triangle circumference. Read the values of the triangle sides from the keyboard. Sample result: + + ``` + Enter a: � + Enter b: � + Enter c: � + Triangle area: � + Triangle circumference: � + ``` + +1. Vehicle registration numbers in Krak�w start with the letters KR or KK. Write a program that checks whether the vehicle registration number entered from the keyboard means a vehicle from Krakow. Display True whether a car is from Krak�w or False otherwise. Sample result: + + ``` + Enter vehicle registration number: KR45091 + Car from Krak�w: True + ``` + +25. People up to and including 26 years of age are exempt from paying taxes in Poland. Write a program that, based on the person's age entered from the keyboard, displays True if the person is exempt from paying taxes and displays False otherwise. Sample result: + + ``` + Enter age: 23 + Exemption from paying taxes: True + ``` + +1. Write a program that checks whether the number entered from the keyboard is even. Display True when the number is even and False when the number is odd. Sample result: + + ``` + Enter number: 34 + Number is even: True + ``` + +1. Write a program that checks whether the number entered from the keyboard is between -10 and 10. Display True if the number is in the given range, and False otherwise. Sample result: + + ``` + Enter number: 17 + Number in the range <-10,10>: True + ``` + +1. Correct body weight has a significant impact on health. Write a program that calculates the Body Mass Index (BMI) based on your height in cm and weight in kg. A user enters data from the keyboard. Find the formula on the Internet for calculating the BMI. Then, using your program, check that you have the correct weight. Display the calculated BMI and display True if the weight is within the valid range, or display False otherwise. Sample result: + + ``` + Enter your height in cm: ... + Enter your weight in kg: ... + Your BMI index: ... + Correct weight: True + ``` + +1. Each programming language provides a set of functions that you can use in your programs. One of them is a function that returns a random number. Write a program that displays results of three dice rolls and the sum of dice rolled. Apply a random number generator: + + + +1. In many games, the numbers one and six on dice have special meaning. Write a program that displays the number of dice rolled and the value True if the number rolled is 1 or 6. Sample result: + + ``` + Dice rolled: 4 + Special number: False + ``` + +1. Write a program that enables a user to challenge a computer. The computer throws dice. Then, the user then tries to guess the number from dice by entering a number from 1 to 6 from the keyboard. If the user has guessed the number from the dice, the computer displays True. Otherwise, the computer displays False. + +1. 23% VAT was paid from an amount. Write a program that reads an amount from the keyboard. Then, it calculates and displays both the amount and its VAT. Apply formatting with two decimal places. Sample result: + + ``` + Amount : 15.84 + VAT 23% : 3.64 + ``` + +1. The password is valid if it is at least 8 characters long. Write a program that checks whether the password read from the keyboard is correct. Sample result: + + ``` + Enter password: qwertyXX + Password is valid: True + ``` + +1. The speed of vehicles on a highway in Poland must be between 40 and 140 km/h. Write a program that checks whether the vehicle speed entered from the keyboard is correct. Sample result: + + ``` + Enter vehicle speed: 158 + Speed is valid: False + ``` + +1. A tree may be cut down if its diameter is at least 50 cm. Write a program that, based on the circumference of the tree entered from the keyboard, calculates and displays the value True if the tree can be cut down, or display the value False otherwise. Sample result: + + ``` + Enter tree circumference: � + Tree can be cut down: � + ``` + +1. A bank buys and sells Euro. Write a program that, based on the Euro buying and selling rates entered from the keyboard, calculates the difference between the buying and selling rates (spread). Display result with 4 decimal places. Sample result: + + ``` + Bank buys EUR: 4.5940 + Bank sells EUR: 4.6250 + Spread: 0.0310 + ``` + +1. In Python, to read a range of characters from the string, a slicing method can be used. + + + + The employee file contains the employee's data in a descriptive form. Write a program in which the personal_data variable contains employee data: + + ``` + "Mr. John May, born on 1998-02-16" + ``` + + Display the employee's name, surname, initials and date of birth on separate lines. Sample result: + + ``` + Description: Mr. John May, born on 1998-02-16 + Name: John + Surname: May + Initials: JM + Born: 1998-02-16 + ``` + +1. To improve readability, telephone numbers are often presented with a dash or space separating some digits. Write a program that displays a 9-digit telephone number entered from the keyboard as three groups of 3 digits each, separated by a dash character. Sample result: + + ``` + Enter phone number: 543097329 + Phone number: 543-097-329 + ``` + +1. The price of the product on the price tag is given before and after the discount is applied. Write a program that allows you to enter the product price and discount. Display the product price, discount, discounted product price, and the difference between the product price before and after the discount. Display all prices with two decimal places. Sample result: + + ``` + Enter price: 24.72 + Enter discount %: 15 + Price with discount: 21.01 + Reduction: 3.71 + ``` + +1. The credit card number consists of 16 digits. Write a program that allows you to enter a credit card number from the keyboard. Display the credit card number in groups of 4 digits, separating the groups with a space character. Sample result: + + ``` + Enter credit card number: 5020312109004442 + Card number: 5020 3121 0900 4442 + ``` + +1. The binary numeral system is a positional numeral system that requires only two digits to write numbers: 0 and 1. The hexadecimal numeral system is a positional numeral system that uses sixteen distinct symbols, most often the symbols "0"�"9" to represent values 0 to 9, and "A"�"F" (or alternatively "a"�"f") to represent values from ten to fifteen. Both are widely used in mathematics, computer science and digital electronics. Write a program that reads an integer number from the keyboard and displays that value as a binary and hexadecimal number. To convert a decimal number to binary or hexadecimal value, use the available Python functions. Sample result: + + ``` + Enter number: 125 + Binary number: 0b1111101 + Hexadecimal number: 0x7d + ``` + +1. Write a program that allows you to enter a binary, four-digit number. Convert the entered number from binary to decimal value. Do not use available Python functions. Sample result: + + ``` + Enter binary number: 0110 + Binary number in decimal notation: 6 + ``` + +1. In computer science, every written (graphic) character of human language (letters, digits, symbols, etc.) is encoded by assigning it a number. This allows characters to be stored, transmitted, and transformed using digital computers. Write a program that displays a numerical representation of each letter of your name. To convert a character to its numerical representation, use the Python ord() function. Sample result: + + ``` + Name: John + J(74) o(111) h(104) n(110) + ``` + + diff --git a/05-Test1/mock/__pycache__/p1.cpython-312.pyc b/05-Test1/mock/__pycache__/p1.cpython-312.pyc new file mode 100644 index 0000000..9a3dee3 Binary files /dev/null and b/05-Test1/mock/__pycache__/p1.cpython-312.pyc differ diff --git a/05-Test1/mock/__pycache__/p2.cpython-312.pyc b/05-Test1/mock/__pycache__/p2.cpython-312.pyc new file mode 100644 index 0000000..febabe1 Binary files /dev/null and b/05-Test1/mock/__pycache__/p2.cpython-312.pyc differ diff --git a/05-Test1/mock/results.txt b/05-Test1/mock/results.txt new file mode 100644 index 0000000..7d4ee69 --- /dev/null +++ b/05-Test1/mock/results.txt @@ -0,0 +1,43 @@ +test_p1 (__main__.Test.test_p1) ... ok +test_p2 (__main__.Test.test_p2) ... ok +test_p3 (__main__.Test.test_p3) ... ERROR +test_p4 (__main__.Test.test_p4) ... ERROR +test_p5 (__main__.Test.test_p5) ... ERROR +test_p6 (__main__.Test.test_p6) ... ERROR + +====================================================================== +ERROR: test_p3 (__main__.Test.test_p3) +---------------------------------------------------------------------- +Traceback (most recent call last): + File "c:\Users\stalj\OneDrive - uek.krakow.pl\Education\PRB\2024-2025-winter\ClassMaterials\05-Test1\mock\check.py", line 27, in test_p3 + import p3 +ModuleNotFoundError: No module named 'p3' + +====================================================================== +ERROR: test_p4 (__main__.Test.test_p4) +---------------------------------------------------------------------- +Traceback (most recent call last): + File "c:\Users\stalj\OneDrive - uek.krakow.pl\Education\PRB\2024-2025-winter\ClassMaterials\05-Test1\mock\check.py", line 32, in test_p4 + import p4 +ModuleNotFoundError: No module named 'p4' + +====================================================================== +ERROR: test_p5 (__main__.Test.test_p5) +---------------------------------------------------------------------- +Traceback (most recent call last): + File "c:\Users\stalj\OneDrive - uek.krakow.pl\Education\PRB\2024-2025-winter\ClassMaterials\05-Test1\mock\check.py", line 37, in test_p5 + import p5 +ModuleNotFoundError: No module named 'p5' + +====================================================================== +ERROR: test_p6 (__main__.Test.test_p6) +---------------------------------------------------------------------- +Traceback (most recent call last): + File "c:\Users\stalj\OneDrive - uek.krakow.pl\Education\PRB\2024-2025-winter\ClassMaterials\05-Test1\mock\check.py", line 43, in test_p6 + import p6 +ModuleNotFoundError: No module named 'p6' + +---------------------------------------------------------------------- +Ran 6 tests in 0.002s + +FAILED (errors=4)