0% found this document useful (0 votes)
17 views

Data Types. Input Data

Uploaded by

suicidbee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Data Types. Input Data

Uploaded by

suicidbee
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Data types.

Input data

Unit 11.1A Basic structures of the Python programming language


Learning objectives
• 11.1.1.3 distinguish between data types in Python;
• 11.1.1.4 convert data types of variables;
• 11.1.1.5 organize keyboard inputs;
• 11.1.1.6 use arithmetic operations when solving problems;
• 11.4.3.2 solve applied problems of various subject areas.
Variables
Variables are containers for
storing data values.
Creating Variables

Python has no command for


declaring a variable.
A variable is created the moment
you first assign a value to it.
• Variables do not need to be declared with any
particular type, and can even change type after they
have been set.
Casting

• If you want to specify the data type of a variable, this can


be done with casting.
Single or Double Quotes?

• String variables can be declared either by using single


or double quotes:
Case-Sensitive
• Variable names are case-sensitive.
Output Variables
• The Python print statement is often used to output variables.
• To combine both text and a variable, Python uses the + character:
Get the Type

• You can get the data type of a


variable with the type() function
• 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.
Type Conversion

You can convert from one type to another with


the int(), float() methods:
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)
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
Keyboard inputs
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);
}
Answer
Adapted Python code:
a=float(input("a="))
b=float(input("b="))
c=float(input("c="))
print("Average value= ",(a + b + c) / 3)
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
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!
Task
• Determine the distance between two points on the
coordinate line.
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))


• 1) Why is the float type used in this task, and not int?
• 2) Why use the abs () function when displaying the distance between
two points.
Practical task
Write a program to calculate the half-perimeter of a triangle if the
length of the sides of this rectangle is known. Formula of half-perimeter
of a triangle
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".
Tasks for practical work on the computer:
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?
Reflectio
n

You might also like