You are on page 1of 7

pupil = "Ben“

old = 16
grade = 9.2
print("It's %s, %d. Level: %f" % (pupil, old, grade))

Results:
It's Ben, 16. Level: 9.200000

Questions:
what do you think %s, %d,%f means?

It's Ben, 16. Level: 9.2


Now let's look at the format() method:

print("This is a {0}. It's {1}.".format("ball", "red"))


This is a ball. It's red.

print("This is a {1}. It's {0}.".format("white", "cat"))


This is a cat. It's white.

print("This is a {2}. It's {0} {1}.".format("a", "number", 1))


This is a 1. It's a number.
name_user = input(‘Your name: ')
city_user = input(‘Your city: ')
print(f’My name’s {name_user}. My city {city_user}')
An f-string is a way to embed expressions inside string literals, using curly
braces {}. When an f-string is prefixed with the letter 'f‘ (hence the name "f-
string"), it allows you to include variables and expressions directly within the
string.
Task 1

Write a program (user.py file) that prompted the user:


- his name (for example, “What is your name?”)
- age (“How old are you?”)
- place of residence (“Where do you live?”)
After this I would conclude with three lines:
"This name"
"This is age"
"(S)he lives in the place of residence
" Instead of name, age and place of residence, user data must be indicated.
Task 2

Write a program (arithmetic.py file) that would ask the user


to solve the example 4 * 100 - 54.
Then it would display the correct answer and the user's
answer. Consider whether you need to convert the string to a
number here.

Task 3

Ask the user for four numbers. Fold the first two separately
and the second two separately. Divide the first amount by
the second. Display the result on the screen so that
the answer contains two digits after the decimal point.
Operator IF ELSE

If boolean_expression
{expression 1; expression 2; … } a=5>0
else
{expression 3;
if a:
… print(a)
}

Operator try-except
You can describe its work in words like this: “Try to do this and that, if an exception occurs,
then do this and that.” Its construction is similar to a conditional statement with an
else branch.
n = input(“Input number: ")
try:
n = int(n)
print(“GOOD")
except:
print(" Something went wrong ")
TASK1
Write a program that prompts you for two values. If at least one of them is not a number,
then concatenation, that is, joining, of strings must be performed. In other cases,
the entered numbers are summed.

Example
First value: 4 First value: a
Second value: 5 Second value: 9
Result: 9.0 Result: a9

You might also like