Data types. Input data
Unit 11.1А: Python programming Basics
Learning objectives
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.
Casting
Single or Double Quotes?�
Case-Sensitive
Output Variables
Get the Type
Type Conversion
You can convert from one type to another with the int(), float() methods:
functions
Consider the following examples of converting functions
print (str (10) + str (20))
print (int ('10 ') + int ('20’))
print (len ('10 ') + len ('20’))
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
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))
Practical task
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?
Reflection