Python. Variables. Data types. Converting data types of variables.

11.1.1.3 distinguish between data types in Python
11.1.1.4 convert data types of variables

Variables in Python. Data types. Converting data types of variables.

Подписывайтесь на канал "Программирование на Python"

Variables

hello = 'Hi'
hello2 = hello
print(hello2)

Rules for creating variables in Python:

  • Variable names must start with a letter or underscore.
  • Variable names cannot start with a number.
  • A variable name can only contain letters, numbers, and underscores (A - z, 0-9, and _).
  • Variable names are case sensitive (num, Num and NUM are three different variables).
  • Reserved words (keywords) cannot be used to name a variable.


number = 10      # variable number of integer type assigns the value 10
pi = 3.14        # floating point pi is assigned the value 3.14
word = "Hello"   # variable word of string type assigns the value "Hello"
print(number, pi, word) # output values of variables

How define the data type of variable.

The function type() shows the data type of variables.

Example

lang = "Python" 
print(type(lang))     # <class 'str'>
amount = 15 
print(type(amount))   # <class 'int'>
height = 1.78 
print(type(height))   #<class 'float'>

You can see that python uses dynamic typing, that is, a variable can change the data type depending on the value assigned to it.

Variable can change value in a program. 

word = "Astana"
print("2017 - ", word)
print("2018 –", word)
word = "Nur-Sultan"
print("2019 –", word)
print("2020 –", word)
print("2021 –", word)

Output data:

2017 - Astana
2018 - Astana
2019 – Nur-Sultan
2020 – Nur-Sultan
2021 – Nur-Sultan

Converting data types

int(string) – convert string to int
float(string) – convert string to float
int(float) – convert floating point number to an integer
str(int) – convert integer to string
str(float) – convert floating point number to string

 

Questions:

1. What does it mean "data type"?

2. Name four data types.

3. Provide examples for each data type.

Exercises

Ex.1 Data types in Python (Author: Mr. Halil Mali - CS teacher of NIS Uralsk) 

Ex.2 Data types in Python


Tasks:

 

Категория: Programming languages | Добавил: bzfar77 (05.09.2021)
Просмотров: 1191 | Теги: String, Boolean, Python, Conversion, integer, variable, Float, data type | Рейтинг: 5.0/1
Всего комментариев: 0
avatar