程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Variables for getting started with Python

編輯:Python

Catalog

One 、 Use of variables

Two 、 The type of variable

3、 ... and 、 Variable name  


One 、 Use of variables

# Variable drill 1--PyCharm
#1. Define a variable record QQ number
qq_number = "111111"
#2. Define a variable record QQ password
qq_password = "123"
# If you want to use the interpreter , Output variable content , Need to use print function
print(qq_number)
print(qq_password)

  Be careful : Executing with the interpreter python When it comes to programming , Variable names cannot be used directly ; Output variable information on the console , You can use variable names directly .
 

# Variable drill 2-- The supermarket buys apples
#1. You can define variables with the results of other variables ;2. After variable definition , It can be used directly in the future
# demand : The price of apple is 8.5 element / Jin , And just buy apples , Just go back 5 element , bought 7.5 Jin apple , Calculate the amount of payment
# Define apple price variables
price = 8.5
# Define purchase weight
weight = 7.5
# Calculate the amount
money = price * weight
# Just buy apples , Just go back to 5 element
money = money - 5
print(money)

Variable names define variables only when they first appear , The variable name appears again , It's not about defining variables , Instead, use the previously defined variables directly . Values stored in variables , It can be changed .

Two 、 The type of variable

Create a variable in memory , Include : The name of the variable 、 Data saved by variables 、 The type of data stored in variables 、 The address of the variable ( identification ).

"""
full name : Xiao Ming
Age :18 year
Gender : It's a boy
height :1.75 rice
weight :75 kg
"""
name = " Xiao Ming "
age = 18
gender = True
height = 1.75
weight = 75

stay python in , When defining variables, you do not need to specify the type of variables , During operation ,python The interpreter will use the data to the right of the equal sign of the assignment statement , Automatically deduce the exact type of data saved in the variable .

Data types can be divided into numeric and non numeric types . Digital : integer (int( stay python 2.x in , Integers are also divided into... According to the length of the saved value :int( Integers )、long( long integer )))、 floating-point (float)、 Boolean type (bool)、 Plural ; Non digital : character string 、 list 、 Tuples 、 Dictionaries .

1. Calculation between different types of variables

1.1 Numerical variables can be calculated directly

stay python in , Two numeric variables can be used for direct arithmetic operation , If the variable is bool type , At the time of calculation ,True The corresponding number is 1,False The corresponding number is 0.

1.2 Use... Between string variables + String concatenation

1.3 String variables can be used with integers * Repeat concatenation of identical strings

 1.4 No other calculations can be performed between numeric variables and strings

2. Variable input

So called input , Is to use code to obtain the information entered by the user through the keyboard ( for example : Go to the bank to get money , stay ATM Enter the password on ), stay python in , If you want to get the user's input information on the keyboard , Need to be used input function .

2.1 About functions

A pre prepared function ( Code written by others or yourself ), You can use it directly , Without paying attention to the internal details .

Functions that have been learned so far :print(x): take x Output to console ;type(x): see x Variable type of .

2.2 input Function to implement keyboard input

stay python Can be used in input Function waits for user input from the keyboard , Anything the user enters python Are considered to be a string .

The grammar is as follows :

 String variable = input(" Prompt information :")

2.3 Type conversion function

int(x): take x Convert to an integer ;

float(x): take x Converts to a floating point number .

2.4 Variable input drill

# Supermarkets buy Apple plus
"""
demand :
The price of apple is entered by the cashier , Company : element / Jin
The cashier enters the weight of the apple the user purchased , Company : Jin
Calculate and output payment amount
"""
# 1. Enter the apple unit price
price_str = input(" Please enter the apple price :")
# 2. Ask for the weight of the apple
weight_str = input(" Please input the apple weight :")
# 3. Calculate the amount
# 1> Convert Apple unit price to decimal
price = float(price_str)
# 2> Convert the apple weight to a decimal
weight = float(weight_str)
# 3> Calculate the amount of payment
money = price * weght
print(money)
# Buy improved apple
# Define a floating-point variable to receive user input at the same time , Just use float Function transformation
price = float(input(" Please enter the price :"))

  The benefits of improvement :

    ① Save space , Just allocate space for one variable ;

    ② Easy to name , You don't need to name intermediate variables .

  Improved “ shortcoming ”:

    ① Beginners need to know , Two functions can be nested , It's a little bit difficult .

2.5 Formatted output of variables

stay python Can be used in print function Output information to console .

If you want to output text messages at the same time , Output data together , You need to use it Formatting operators .

% go by the name of Formatting operators , It is specially used to handle the format in string .

  contain % String , It's called Formatted string .

  % And different character Continuous use , Different types of data Need to use Different formatting characters .

The syntax is as follows :

print(" Formatted string " % Variable 1)
print(" Formatted string " % ( Variable 1, Variable 2...))
# Format output walkthrough -- Basic exercises
# demand
"""
1. Defining string variables name, My name is Xiao Ming , Please take good care of me !
2. Define integer variables student_no, Output my student number is 000001
3. Define decimals price、weight、money, Output Apple unit price 9.00 element / Jin , bought 5.00 Jin , Need to pay 45.00 element
4. Define a decimal scale, The output data scale is 10.00%
"""
print(" My name is %s, Please take good care of me !" % name)
print(" My student number is %06d" % student_no)
print(" Apple unit price %.02f element / Jin , Buy %.02f Jin , Need to pay %.02f element " % (price,weight,money))
print(" The data scale is %.02f%%" % (scale * 100))

3、 ... and 、 Variable name  

1. Identifiers and keywords

1.1 identifier

An identifier is a programmer defined variable name 、 Function name , The identifier can be defined by Letter 、 Underline and numbers form , Cannot start with a number , Cannot have the same name as the keyword .

1.2 keyword

The keyword is in python Identifier already used internally , Keywords have special functions and meanings , developer It is not allowed to define an identifier with the same name as the keyword .

2. Naming rules for variables

Naming rules can be seen as a convention , There is no absoluteness or compulsion , The purpose is to increase the recognition and readability of the code , Be careful python Identifiers in are case sensitive .

When you define variables , To ensure the code format ,= There should be a space on the left and right of each .

stay python in , If the variable name needs two or more words , You can name... In the following way : Use lowercase letters for each word ; Use... Between words _ Underline connection ; for example :first_name、last_name、qq_number、qq_password.

Hump nomenclature

When the variable name is composed of two or more words , You can also use the hump nomenclature to name .

Little hump nomenclature : The first word begins with a lowercase letter , Capitalize subsequent words , for example :firstName、lastName.

Big hump nomenclature : The first letter of every word is capital , for example :FirstName、LastName.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved