1 of 29

Data types. Input data

Unit 11.1А: Python programming Basics

2 of 29

Learning objectives

  • 11.1.1.3 distinguish between data types;
  • 11.1.1.4 convert data types;
  • 11.1.1.5 arrange data input from the keyboard;
  • 11.1.1.6 use arithmetic operations when solving problems.

3 of 29

Variables

Variables are containers for storing data values.

4 of 29

Creating Variables

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

5 of 29

  • Variables do not need to be declared with any particular type, and can even change type after they have been set.

​

6 of 29

Casting

  • If you want to specify the data type of a variable, this can be done with casting.

​

7 of 29

Single or Double Quotes?�

  • String variables can be declared either by using single or double quotes:

​

8 of 29

Case-Sensitive

  • Variable names are case-sensitive.

​

9 of 29

Output Variables

  • The Python print statement is often used to output variables.
  • To combine both text and a variable, Python uses the + character:

​

​

10 of 29

Get the Type

  • You can get the data type of a variable with the type() function

​

11 of 29

  • In the program, the user used one variable to work with different data.
  • Determine how the type of the variable “value” changes during program execution.

​

​

12 of 29

Type Conversion

You can convert from one type to another with the int(), float() methods:

13 of 29

functions

  • int (“text”) - converts a string to an integer
  • float (“text”) - converts a string to a real number
  • str (num) - Convert a number to a string
  • abs (num) - absolute value of a number
  • len (“text”) - the length of the string (the number of characters in the string)

14 of 29

Consider the following examples of converting functions

print (str (10) + str (20))

print (int ('10 ') + int ('20’))

print (len ('10 ') + len ('20’))

15 of 29

Consider the following examples of converting functions

print (str (10) + str (20)) # prints '1020'

print (int ('10 ') + int ('20’)) # prints 30

print (len ('10 ') + len ('20’)) # prints 4

16 of 29

17 of 29

Keyboard inputs

18 of 29

Adapt code written in C# using the keyboard input operator in Python.

C# code:

private void button1_Click(object sender, EventArgs e)

{

float a, b, c;

a = Convert. ToSingle(textBox1.Text);

b = Convert. ToSingle(textBox2.Text);

c = Convert. ToSingle(textBox3.Text);

label1. Text = "Average value = "+Convert. ToString((a + b + c) / 3);

}

19 of 29

Answer

Adapted Python code:

a=float(input("a="))

b=float(input("b="))

c=float(input("c="))

print("Average value= ",(a + b + c) / 3)

​

20 of 29

Simplest arithmetic operations

Operator

Name

Example

+

Addition

x + y

-

Subtraction

x - y

*

Multiplication

x * y

/

Division

x / y

%

Modulus

x % y

**

Exponentiation

x ** y

//

Floor division

x // y

21 of 29

Practical task (questionnaire program)

Write a program – questionnaire. To do this, ask the username, surname, age, hobby, favorite subject at school, favorite color, favorite game, favorite movie. And output in the format: My name is (surname) + (name), I am (age) years old, I like to do (hobby), my favorite subject is (subject), favorite color: (color), favorite game (game), and favorite movie (film). It was nice to meet you!

​

22 of 29

Task

  • Determine the distance between two points on the coordinate line.

23 of 29

Answer

print(" Enter the coordinate of the first point :")

point1 = float(input())

print(" Enter the coordinate of the second point :")

point2 = float(input())

s = point2 - point1

print(“Distance =", abs(s))

​

24 of 29

  • Why is the float type used in this task, and not int?
  • Why use the abs () function when displaying the distance between two points.

25 of 29

Practical task

  •  

26 of 29

Make a program for converting the temperature according to Fahrenheit to the temperature in degrees Celsius and vice versa.

For example, the user input the number tf=100, the program gives the result: "Temperature Celsius = 37.8", when entering tc = 40, "Temperature Fahrenheit = 104".

27 of 29

Tasks for practical work on the computer:

28 of 29

Questions?

  1. What extension do Python files have?
  2. What alphabet is used in Python?
  3. What is programming language syntax?
  4. What does translator mean?
  5. What is the meaning of the word "int"?
  6. What is the function to input data from the keyboard.
  7. What are data types?
  8. What sign is used when calculating the remainder of a division?
  9. What sign is used when calculating the whole part of the division?

29 of 29

Reflection