Variable
- A variable is a storage unit on the computer’s memory that stores values that can be used for processing calculations.
- Variables can store data in the form of strings, numbers, etc.
- By assigning different data types to the variable, we will create variables of type integer, decimal, string, etc.
- Variables need to be declared before being used.
- Syntax for declaring and assigning values to variables: Variable name = <value>.
- We can declare and assign values to many variables at the same time, the syntax:
biến_1
,biến_2
,biến_3…
=<giá_trị_1>
,<giá_trị_2>
,<giá_trị_3>
…
Identifier
- When programming, we often name variables (variables), methods / functions (functions), classes (classes), modules and other objects. Such naming is called identifier.
- Identifiers are started with the characters A – Z, a – z, or _, followed by alphanumeric characters 0 – 9. Identifier in Python is case sensitive, especially when naming, Python does not use punctuation characters like @, #, $, % …
- When naming variables, it is necessary to give them a meaningful and highly reminiscent name. In case the variable name consists of many words, usually we will have 2 ways to use the name.
Way 1: Voices separated by underscores _
: ten_hoc_sinh
, nghiem_phuong_trinh
.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
Way 2: Written in “camel” style, the first letter of each word will be capitalized: TenHocSinh
, NghiemPhuongTrinh
.
Some rules of identifiers:
– Class names start with a capital letter. All other identifiers begin with a lowercase letter.
– Function names are lowercase, words are connected by underscores _
.
– Do not use keywords in Python when naming any identifier, like: and
, assert
, break
, class
, continue
, def
, del
, elif
, else
, except
, exec
, finally
, for
, from
, global
, if
, import
, in
, is
, lambda
, not
, or
, pass
, print
, raise
, return
, try
, while
, with
, yield
…
Examples of variable naming:
– 1_gia_tri
: invalid variable name because the starting character is a numeric character.
– nghiem_x1
: valid variable name, clearly delimited using underscores.
–
ty_gia_$
: the variable name is invalid because the variable name contains a special character $.
– tenhocsinh
: valid variable name, but should separate the sounds in two ways using underscores or writing “camel” as above.
– print
: invalid variable name because it coincides with the “print” function keyword in Python.
Exercise
Below are some basic programming exercises on variables and identifiers, basic calculations to help you get acquainted and better understand Python programming operations. Any questions, please comment below.
Lesson 1:
Write a program to calculate the amount of goods according to the formula: Thành tiền = Số lượng * Đơn giá
.
Description: Enter the quantity and unit price data with the command input()
by data type integer (int) into 2 variables so_luong
and don_gia
. Print to the screen with the command print()
value to money by multiplying the quantity by the unit price.
Lesson 2:
Write a program to calculate the area and perimeter of a circle.
Description: Because calculating the area and circumference of a circle, we need to use the constant pi in mathematics, so we need to use the import command to declare the math library used in mathematics. Enter the circle radius value with the command input()
with datatype integer (int) into variable ban_kinh
.
Calculate the circumference of the circle according to the formula: twice the radius multiplied by the pi value, the pi value will be taken from the library math
with syntax: math.pi
the value of the circumference of the circle after calculating will be assigned the value to the variable p
. Similarly, we also calculate the area of a circle according to the formula: square of the radius multiplied by pi, the area value will then be assigned to the variable. s
.
Use function print()
to print the circumference and area of the circle, these two values will be formatted as real numbers float (f) and rounded to two decimal places (%.2f và %p
) (Will be detailed in Chapter 3).
lesson 3
Write a program to convert the temperature from Celsius to Fahrenheit.
Description: Enter the temperature C with integer type (integer – int) into the variable C with the input() command. Calculate the conversion value to temperature F using the formula: 9/5 * temperature C +32, and assign this value to the variable F.
Use function print()
To print the results, the two temperature values C and degrees F are both real numbers (float – f) and are rounded to 2 decimal places. The value pair %(C,F) represents the order in which to print the values according to the % sign in printing the results.
Lesson 4
Write a program to process the required string:
- The string s1, s2, s3 is entered from the keyboard.
- The index index is entered from the keyboard.
- Give the lengths of the strings s1, s2, and s3.
- Create substring s4 from string s1 with content from Index to end of string.
- Repeat the sequence s2: 2 times.
Description: Use command input()
input string data (string – str) into variables s1, s2, s3. Enter the index value as an integer (integer – int) into the index variable. The length of the strings is calculated using the function len()
. Command s1[index:]
used to create a new string starting from index position to the end of string s1. Creating a new string double by multiplying operator *.
Lesson 5
Write a program to calculate interest on a savings account
Calculating savings deposit interest: 1-year interest rate, deposit amount and number of deposit months are entered from the keyboard. Write a program that calculates interest and calculates the total amount received after the deposit period expires:
Interest = (Deposit amount * Number of months) * (Year/12) ;
Total amount = Deposit amount + Interest.
Then display the results.
Description: Enter real interest (float), deposit, and integer month (integer – int) values into variables lai_suat
, so_tien_gui
and so_thang
. Interest is calculated according to the given formula and assigned to the variable so_tien_lai
. Add the amount of deposit and the amount of interest just calculated into the variable tong_tien
. Use command print()
to print the results.
Note: Articles compiled by you Le Tuan Thuan has the goal of introducing and supporting teaching for students or beginners who want to familiarize themselves with and learn the Python programming language, so the commands and content are simple and easy to understand, easy to read.
References:
- Textbook “Self-learning basic Python programming” (2019); Vu Hai Quan, Cao Xuan Nam, Hoang Trung Hieu, Nguyen Hai Trieu, Vong Chi Tai; Artificial Intelligence Laboratory (AILab), University of Natural Sciences (VNU-HCM); Publishing House of the National University of Ho Chi Minh City. Ho Chi Minh.
- Learning material “Python Programming Basics” (2017); Informatics Center, University of Natural Sciences, Ho Chi Minh City.