Python first day, python first day

Source: Internet
Author: User

Python first day, python first day

I. variable naming rules

1. Variable names can only be any combination of letters, numbers, and underscores

2. The variable name cannot start with a number.

3. Some reserved fields cannot be used as custom variable names

4. The variable name must have a clear meaning. For example, it is best to define the variable name as name.

 

Ii. character encoding

Character encoding is used to convert input characters into machine codes. ASCII code is the earliest character encoding in the world. The earliest character encoding in China is GB2312, which contains more than 7000 Chinese characters and symbols. After the first extension, it contains more than 20 thousand Chinese characters, the character encoding is GBK1.0. After the second expansion, more than 27000 Chinese characters are included and the character encoding is GB18030. in order to unify the character encoding, the Unicode character encoding is introduced, because this encoding requires more storage space than the original ASCII encoding, after compression optimization launched the UTF-8 character encoding, this encoding is still one character a byte when stored in English, A Chinese character is stored in three bytes.

 

3. Python user interaction program

Python has a variety of interactive programs, also known as interpreters. The most common interaction program is CPython, and the fastest code running speed is PyPy.

 

IV. if judgment statement

#! /Usr/bin/env python
Age_of_oldboy = 56
Guess_age = int (input ("guess_age :"))
If guess_age = age_of_oldboy:
Print ("yes, you got it .")
Elif guess_age> age_of_oldboy:
Print ("think smaller ")
Else:
Print ("think bigger ")

The purpose of this Code is to allow the user to enter a number to guess the value of age_of_oldboy. Note that in Python, when a line of code has no subordination with the above Code, you need to enter the top level, when there is a subordination, a certain degree of indentation is required to indicate the subordination. Generally, it is Indented by four cells. In addition, special statements such as if, elif, else, while, and for that indicate judgment and loop must end with a: sign. This code is written in the python3.X environment. Do not add () to print in python2.X. In python2.X, replace input () with raw_input ().

 

5. while Loop

Age_of_oldboy = 56

Count = 0
While count <3:
Guess_age = int (input ("guess_age :"))
If guess_age = age_of_oldboy:
Print ("yes, you got it ..")
Break
Elif guess_age> age_of_oldboy:
Print ("think smaller ...")
Else:
Print ("think bigger ")
Count + = 1
If count = 3:
Continue_confirm = input ("do you want to guessing ...")
If continue_confirm! = 'N ':
Count = 0

Break indicates that the current loop is ended, and countinue indicates that the next loop is skipped. After a while loop, you can write an else statement. When the loop ends normally, the action under the else statement is executed, such as break and countinue.

6. for Loop

For count in range (3 ):
Guess_age = int (input ("guess_age :"))
If guess_age = age_of_oldboy:
Print ("yes, you got it ..")
Break
Elif guess_age> age_of_oldboy:
Print ("think smaller ...")
Else:
Print ("think bigger ")

Else:
Print ("sorry, you try too mary times ..")

 

For I in range (0, 10, 2): # range (initial value, condition, step size)
Print (I)

 

7. Method for referencing variables

1 use placeholders

Name = input ("name :")
Age = input ("age :")
Job = input ("job :")
Salary = input ("salary :")
Info = ''' ----- info of % s -----
Name: % s
Age: % s
Job: % s
Salary: % s
''' % (Name, name, age, job, salary)

The placeholder % s is used to indicate that the position needs to be filled with a string. The following % is followed by the string used to fill the position of the placeholder. variables can be referenced at this position. Other placeholders include % d and % f, which respectively indicate filling with Integer type and floating point type.

Info2 = ''' ----- info of {_ name }-----
Name: {_ name}
Age: {_ age}
Job: {_ job}
Salary: {_ salary}
'''. Format (_ name = name, _ age = age, _ job = job, _ salary = salary)

Info3 = ''' ----- info of {0 }-----
Name: {0}
Age: {1}
Job: {2}
Salary: {3}
'''. Format (name, age, job, salary)

 

8. The role '''

1 multi-line comment

''' ----- Info of {0 }-----
Name: {0}
Age: {1}
Job: {2}
Salary: {3}
'''

2 multi-row output

Info3 = ''' ----- info of {0 }-----
Name: {0}
Age: {1}
Job: {2}
Salary: {3}
'''

Print (info3)

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.