Python Basics 3-Basic use and naming of variables

Source: Internet
Author: User
Tags compact true true unsupported

4. Basic use of Variables 4.1 variable definition
    • In Python, each variable must be assigned before it is used, and the variable will be created after the variable is assigned .
    • The equals sign (=) is used to assign a value to a variable
      • = Left is variable name
      • = right is the value stored in the variable
Variable name = value

After the variable is defined, the following can be used directly

4.2 Types of variables
    • Creating a variable in memory will include:

Name, saved data, type of stored data, address (label)

    • Defining a variable in Python does not require a specified type (required in many other high-level languages)
    • Data types can be divided into digital and non-digital
    • Digital type
      • Integral type (int)
      • Float type (float)
      • Boolean type (BOOL)
        • True true not 0 number--nonzero is true
        • False false 0
      • Complex Type (complex)
        • Mainly used for scientific calculation, such as: Plane field problem, fluctuation problem, inductance capacitance and so on.
    • Non-digital type
      • String
      • List
      • Meta-group
      • Dictionary

Tip: In Python 2.x, integers are divided according to the length of the saved values:

      • Int (integer)
      • Long (length integer)
      • You can see the type of a variable using the type function
In [1]: type (name)

  

4.3.1 calculation between different types of variables

1) The number of variables can be calculated directly between

    • In Python, two numeric variables are available for direct arithmetic operations.
    • If the variable is of type bool, at the time of calculation
      • True the corresponding number is 1
      • False the corresponding number is 0

2) use + stitch string between string variables

    • In Python, a new string can be generated using + stitching between strings
In [1]: first_name = "three" in [2]: last_name = "Zhang" in [3]: First_Name + last_nameout[3]: ' Three sheets '

  

3) string variables can be used with integers * to repeatedly stitch the same string

In [1]: "-" * 50out[1]: '--------------------------------------------------'

  

4) No other calculations between a numeric variable and a string

In [1]: first_name = "Zhang" in [2]: x = 10In [3]: x + first_name---------------------------------------------------------- -----------------typeerror:unsupported operand type (s) for +: ' int ' and ' str ' types error: ' + ' unsupported operation type: ' int ' and ' str '

  

Input of the 4.3.2 variable
    • The so-called input , is to use code to get the user input information through the keyboard
    • In Python, if you want to get the user's input on the keyboard , you need to use the input function

Function

Description

Print (x)

Output x to the console

Type (x)

View the variable type of x

    • In Python, you can use the input function to wait for the user's input from the keyboard
    • any content entered by the user Python is considered a string
String variable = input ("hint message:")

  

type conversion function

Function

Description

int (x)

Convert x to an integer

Float (x)

Convert x to a floating-point number

defines a floating-point variable to be converted using the FLOAT function while receiving user input

Price = Float (Input ("Please enter prices:"))

  

Formatted output of 2.5 variables

Apple price 9.00 yuan/catty, purchased 5.00 pounds, need to pay 45.00 yuan

    • Use the print function to output formatted content
    • % is called the format operator , which handles string formatting
      • contains a string of%, is Called format string
      •  % with different character , different types of data need to use Different formatting characters

format character

%s

string

< P class= "compact" >%d

signed decimal integer,%06d indicates the number of digits of the output, 0 complement

%f

floating-point number,%.2f indicates only two digits after the decimal point /p>

%%

output%

Print ("formatted string"% variable 1) print ("formatted string"% (variable 1, variable 2 ...)) Print ("My name is%s, please take care!") "% name" print ("My school number is%06d"% student_no) print ("Apple unit price%.02f yuan/kg, purchase%.02f Catty, need to pay%.02f yuan"% (prices, weight, money)) print ("Data Ratio is%.02f%% "% (scale * 100))

  

Practice-- Personal Card

Demand

    • Prompt user input in the console: name , company , position , phone , email
    • Output in the following format:
Company name Name (position) Tel: e-mail: Email **************************************** **********

  

Code:

"" In the console prompts the user to enter: name, company, position, phone, email "" "Name = Input (" Enter name: ") company = input (" Please enter Company: ") title = input (" Please enter position: ") phone = input ("Please enter the phone:") email = input ("Please enter the mailbox:") Print ("*" *) print ("*" * "*") "Print" ("%s"% (name, title)) Print ("Tel: %s "% phone" print ("Mailbox:%s"% email) print ("*" * 50)

  

5. Name of the variable 5.1 identifier

Identifiers are programmer-defined variable names , function names

    • Identifiers can consist of letters , underscores , and numbers
    • Cannot start with a number
    • cannot be duplicate with keyword
    • The keyword is the identifier that has been used inside Python
    • keywords have special features and meanings
    • Developers are not allowed to define identifiers with the same name as keywords
5.2 Key Words

Use the following command to view the keywords in Python

In [1]: Import Keywordin [2]: print (keyword.kwlist)

  

    • Import keyword can be imported into a " Toolkit"
    • Different toolkits in Python, with different tools available
5.3 Naming conventions for variables

naming rules can be considered as a convention , and there is no absolute and mandatory purpose to increase the identification and readability of code

Note identifiers in Python are case-sensitive

    1. When defining variables, the left and right of the = should keep a space
    2. In Python, you can name it in the following ways
      1. Use lowercase letters for each word
      2. Use the underline connection between words and words

For example: first_name, last_name, Qq_number, Qq_password

Hump Naming Method

    • The small hump-type naming method

The first word starts with a lowercase letter, and the first letter of the subsequent word is capitalized. For example: FirstName, LastName

    • The large hump-type naming method

The first letter of each word is capitalized. For example: FirstName, LastName, CamelCase

Python base 3-basic use and naming of variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.