Thứ Tư, Tháng Hai 8, 2023
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
NATuts
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
No Result
View All Result
NATuts
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z
No Result
View All Result
NATuts
No Result
View All Result
Home Tech

Chapter 2: Variables and Identifiers – Python Basics

23 Tháng Chín, 2022
in Tech
0
Chapter 2: Variables and Identifiers – Python Basics
585
SHARES
3.2k
VIEWS
Share on FacebookShare on Twitter

Các bài viết liên quan:

How to get travel insurance

Guide on how to get travel insurance with 4 options

24 Tháng Một, 2023
Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

8 Tháng Một, 2023
5 Best Software to Stream Games

5 Best Software to Stream Games

2 Tháng Một, 2023
IBM Bridge To Cloud For Power

IBM Bridge To Cloud For Power- Everything You Should Know

2 Tháng Một, 2023
Top 10 CRM Software For Construction 

Top 10 CRM Software For Construction Enterprises All The Time

31 Tháng Mười Hai, 2022
What Is IBM Software

What Is IBM Software? 4 Business Segments at IBM You Should Know

26 Tháng Mười Hai, 2022

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.

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

Describe: 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 9

Lesson 2:

Write a program to calculate the area and perimeter of a circle.

Describe: 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 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 10

lesson 3

Write a program to convert the temperature from Celsius to Fahrenheit.

Describe: 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 11

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.

Describe: 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 12

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.

Describe: 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 13

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); National University Publishing House. Ho Chi Minh.
  • Learning material “Python Programming Basics” (2017); Informatics Center, University of Natural Sciences, Ho Chi Minh City.
Previous Post

Gaming Influencers on YouTube – Free PC Technology

Next Post

Four Ukrainian provinces may soon join Russia, Kremlin says

Megusta

Megusta

Related Posts

5 Best Software to Stream Games

5 Best Software to Stream Games

2 Tháng Một, 2023
Top 10 CRM Software For Construction 

Top 10 CRM Software For Construction Enterprises All The Time

31 Tháng Mười Hai, 2022
Instruction how to use OBS streaming software

Features, settings and how to use OBS streaming software through 9 simple steps

25 Tháng Mười Hai, 2022
What is Trans woman?  What is Transgender Women?

What is Trans woman? What is Transgender Women?

23 Tháng Mười Hai, 2022
Christmas gift: Genuine Windows 10 Pro for only $6.63 and Office 2021 for $14.22

Christmas gift: Genuine Windows 10 Pro for only $6.63 and Office 2021 for $14.22

22 Tháng Mười Hai, 2022
How to get 50 free coins of SkyJoy App to redeem

How to get 50 free coins of SkyJoy App to redeem

21 Tháng Mười Hai, 2022
Load More
Next Post
Four Ukrainian provinces may soon join Russia, Kremlin says

Four Ukrainian provinces may soon join Russia, Kremlin says

Trả lời Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Bài viết mới

How to get travel insurance
Đời sống

Guide on how to get travel insurance with 4 options

24 Tháng Một, 2023
Software Asset Management for Websites: How to Keep Your Sites Running Smoothly
Phần mềm

Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

8 Tháng Một, 2023
5 Best Software to Stream Games
Software

5 Best Software to Stream Games

2 Tháng Một, 2023
IBM Bridge To Cloud For Power
Software

IBM Bridge To Cloud For Power- Everything You Should Know

2 Tháng Một, 2023
Top 10 CRM Software For Construction 
Tech

Top 10 CRM Software For Construction Enterprises All The Time

31 Tháng Mười Hai, 2022
What Is IBM Software
Software

What Is IBM Software? 4 Business Segments at IBM You Should Know

26 Tháng Mười Hai, 2022
W3Schools

Ads

Contact: [email protected]

DMCA.com Protection Status

Categories

  • Android
  • Cạm bẫy tâm lí
  • Chưa được phân loại
  • Đồ họa
  • Đời sống
  • Gen Z
  • Health
  • iOS
  • Kĩ năng mềm
  • News
  • Nhà mạng
  • Phần mềm
  • Phần mềm đồ họa
  • Review sách
  • Software
  • Tech
  • Thiết kế ảnh
  • Thiết kế video
  • Thủ thuật
  • Travel
  • Văn hóa Nam Bộ
  • Văn học
  • Window

Browse by Tag

ai là triệu phú android Apple browser Bullet Journal bản thân chai pin Chỉnh ảnh data domain download fshare game game show giả lập màu hosting IKEA ios khuyến mãi kinh doanh kiến thức kiểm tra pin messenger miễn phí mua sắm Máy ảnh mạng network nghệ thuật ngôn ngữ nhà Trần pin laptop quảng cáo tiếng anh trạng thái Trần Thủ Độ tên miền tắt hoạt động từ vựng video viettel window 10 word zalo Đơn giản

Recent News

How to get travel insurance

Guide on how to get travel insurance with 4 options

24 Tháng Một, 2023
Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

Software Asset Management for Websites: How to Keep Your Sites Running Smoothly

8 Tháng Một, 2023

Trang tin nóng hổi - vừa thổi vừa xem

No Result
View All Result
  • Home
  • Health
  • News
  • Software
  • Tech
  • Travel
  • Gen Z

Trang tin nóng hổi - vừa thổi vừa xem