• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

Chapter 2: Variables and Identifiers – Python Basics

AnonyViet by AnonyViet
January 24, 2023
in Tips
0

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.

Chapter 2: Variables and Identifiers - Python Basics 7

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.pithe 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).

Chapter 2: Variables and Identifiers - Python Basics 8

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.

Chapter 2: Variables and Identifiers - Python Basics 9

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 *.

Chapter 2: Variables and Identifiers - Python Basics 10

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.

Chapter 2: Variables and Identifiers - Python Basics 11

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.

The article achieved: 5/5 – (100 votes)

Tags: BasicsChapterIdentifiersPythonVariables
Previous Post

Driver Booster 9 Pro Full Key – Automatic Driver Update for Windows

Next Post

Lesson 213: Random list in Excel

AnonyViet

AnonyViet

Related Posts

[Godot Shooter] #2: Creating characters & shooting bullets
Tips

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025
What do you need to learn game programming? Is it difficult? How long does it take?
Tips

What do you need to learn game programming? Is it difficult? How long does it take?

June 6, 2025
Instructions for registering chatgpt team at $ 1
Tips

Instructions for registering chatgpt team at $ 1

June 5, 2025
How to engrave the right mouse menu error on Windows
Tips

How to engrave the right mouse menu error on Windows

June 5, 2025
How to create online meme photos is very easy with a few steps
Tips

How to create online meme photos is very easy with a few steps

June 5, 2025
[Godot Engine] Export to Windows, Linux, MacOS, Android
Tips

[Godot Engine] Export to Windows, Linux, MacOS, Android

June 4, 2025
Next Post
Lesson 213: Random list in Excel

Lesson 213: Random list in Excel

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

[Godot Shooter] #2: Creating characters & shooting bullets

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025

Tải App 89Bet Để Trải Nghiệm Không Giới Hạn

June 6, 2025
What do you need to learn game programming? Is it difficult? How long does it take?

What do you need to learn game programming? Is it difficult? How long does it take?

June 6, 2025
Guide to search law with AI quickly and accurately

Guide to search law with AI quickly and accurately

June 6, 2025
[Godot Shooter] #2: Creating characters & shooting bullets

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025

Tải App 89Bet Để Trải Nghiệm Không Giới Hạn

June 6, 2025
What do you need to learn game programming? Is it difficult? How long does it take?

What do you need to learn game programming? Is it difficult? How long does it take?

June 6, 2025
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

[Godot Shooter] #2: Creating characters & shooting bullets

[Godot Shooter] #2: Creating characters & shooting bullets

June 7, 2025

Tải App 89Bet Để Trải Nghiệm Không Giới Hạn

June 6, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí SHBET https://kubet88.yoga/ bj88

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí SHBET https://kubet88.yoga/ bj88

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply