Tasks studies - laboratory
Let’s start by creating a short program that displays “Hello world!”. In Python, we use the print
command to display text. Below is a ready-to-use script.
print('Hello world!')
For comparison, below is a program written in Java:
```Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
As you can see, the Java program performs exactly the same function but is much more “complicated.” Therefore, programming in Python is simpler.
Python can perform calculations. Enter calculations directly into the Python console, and it will return the result.
>>> 2 + 2
4
>>> 5 - 2 + 4
7
>>> (-7 + 2) * (-4)
20
>>> 6/0
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
6/0
ZeroDivisionError: integer division or modulo by zero
In Python, the last line of the error message informs you about the type of error. :exclamation:
Numeric Operator | Description |
---|---|
x + y | sum of x and y |
x - y | difference of x and y |
x * y | product of x and y |
x / y | quotient of x and y |
x // y | (floor division) quotient of x and y |
x % y | remainder of x / y |
abs(x) | absolute value of x |
divmod(x, y) | pair (x // y, x % y) |
pow(x, y) | x raised to the power of y |
x ** y | x raised to the power of y |
x += y | x = x + y |
x -= y | x = x - y |
x *= y | x = x * y |
x /= y | x = x / y |
Comparison Operator | Description |
---|---|
x != y | not equal |
x == y | equal |
x > y | greater than |
x < y | less than |
x >= y | greater than or equal to |
x <= y | less than or equal to |
Many other languages have special operators, such as “++” as a shortcut for “x += 1”. Python does not have these.
When creating a script that prints “Hello world!” to the screen, the text was enclosed in single quotes (‘text’). The same effect can be achieved when the text is enclosed in double quotes (“text”). If a string requires an apostrophe (e.g., He’s), you can place a backslash (\
) before the apostrophe. Try out these constructions.
The backslash can also be used for tabs, newline characters, any Unicode characters, and various other things that cannot be printed directly.
The concatenation operator +
is used to join strings. It does not matter whether the strings are created using single or double quotes. Strings can also be multiplied by an integer, e.g., "spam" * 4
. Try to see the result of this operation. The indexing operator is also useful, as shown in the script below. Run it in the Python environment and check the result.
a = "Welcome to Python's world!"
print(a[0])
print(a[0:7])
A useful function is the ability to take user input using the input
function, which has the following form:
input("Enter your message: ")
This means converting a variable from one type to another, e.g., float(2)
converts an integer to a floating-point number.
Variables play a very important role in most programming languages, and Python is no exception. A variable allows storing a value by assigning it to a name, which can be used to reference the value later in the program. To assign a variable, use a single equals sign.
>>> x = 7
>>> print(x)
7
>>> print(x + 3)
10
>>> print(x)
7
A variable can be reassigned multiple times to change its value.
>>> x = 123
>>> print(x)
123
>>> x = "This is a string"
>>> print(x + "!")
This is a string!
In Python, variables do not have predefined types, so you can assign a string to a variable and later assign an integer to the same variable.
There are some restrictions on the characters that can be used in Python variable names. Only letters, numbers, and underscores are allowed. Additionally, they cannot start with a number. Violating these rules results in errors.
valid_variable_name = 7
123abc = 7
SyntaxError: invalid syntax
Python is case-sensitive. This means that
Lastname
andlastname
are two different variable names in Python.
Attempting to reference a variable that has not been assigned a value results in an error. You can use the del
statement to delete a variable, meaning that the reference from the name to the value will be removed, and attempting to use the variable will cause an error.
>>> foo = "string"
>>> foo
'string'
>>> bar
NameError: name 'bar' is not defined
>>> del foo
>>> foo
NameError: name 'foo' is not defined
The variables
foo
andbar
are called metasyntactic variables, meaning they are used as placeholders in example code to demonstrate something.
:one: Test a few basic calculations (+
, -
, *
, /
). Try performing different calculations using different operators.
:two: Write a script that asks the user for their name and then displays a greeting using the provided name.
:three: Write a short script (one line of code is enough) that calculates the sum of integers entered by the user and displays the result as a floating-point number.
:four: Using the range
and sum
functions, calculate the sum of natural numbers from 8 to 80. (Refer to the documentation for these functions.)
:five: Write a script that calculates the number of days from a given date (format: yyyy-mm-dd
) to the current date. Use: datetime module documentation.
:six: Using your acquired knowledge, write a simple calculator with a menu for selecting operations and performing operations on two numbers.