0603 Python Basics 02

Source: Internet
Author: User
Tags control characters ming

The difference between job 1:ansi and UTF8?
ASCII is an encoding specification used to denote English characters, which occupy 1 bytes (8bits) per ASCII character.
The maximum number of characters that can be represented is 256, typically only the first 128 (the highest bit is 0), which includes control characters, numbers, uppercase and lowercase letters, and some other symbols.
ANSI: Standard text storage format for system presets. ANSI is the abbreviation for American National Standards Institute.
Unicode: A collection of all the major instruction files in the world, including common word sets used by commercial and personal computers.
Utf-8:utf means the universal Character Set conversion format (Universal Character set Transformation format), and UTF-8 is the Unicode 8-bit format.

A word of advice: when it comes to compatibility considerations, do not use Notepad and use a professional text editor to save as a UTF-8 without a BOM.
If this is for cross-platform compatibility, just be aware that in the context of Windows Notepad:
The so-called "ansi" refers to the Legacy (Legacy) encoding that corresponds to the current system locale.
The so-called "unicode" refers to small-order UTF-16 with a BOM.
The so-called "utf-8" refers to the UTF-8 with a BOM.

GBK such as legacy coding is the most troublesome, so don't use it unless you know what you're doing.
UTF-16 theory is actually very good, byte order is also marked, but UTF-16 is not used.
UTF-8 is originally the best compatibility encoding but Windows to add BOM so often problems.

So, the best cross-platform compatibility is actually not using Notepad.
It is recommended to use normal professional text editor such as notepad++ to save as UTF-8 without BOM.

Also, if all the characters in the text are within the ASCII range, the so-called "ansi" file saved by Notepad is the same as the ASCII or non-BOM UTF-8.

With regard to the character set (character set) and encoding (encoding), there seems to be some confusion in some of the answers.

For legacy scenarios such as ASCII, GB 2312, BIG5, GBK, GB 18030, basically a character set scheme uses only one encoding scheme.
For example, the ASCII standard itself directly specifies the way character and character encoding, so both the character set and encoding scheme, and GB 2,312 is only a location code form of the character set standard, but in fact basically used EUC-CN to encode, so mention "GB 2312"is also said to be a character set and encoding scheme, and GBK and GB 18030 are similar to the backward compatibility with GB 2312.
As a result, many people are affected by these legacy programs and cannot understand the relationship between character sets and encodings.

For Unicode, the character set and encoding are clearly distinguished. The UNICODE/UCS standard is first of all a unified character set standard. The UNICODE/UCS standard also defines several optional coding schemes, called "encoding form" in standard documents, including UTF-8, UTF-16, and UTF-32.
Therefore, for the Unicode scheme, the same text based on the Unicode character set can be stored and transmitted in multiple encodings.
Therefore, using "unicode" to address an encoding scheme is inappropriate and misleading.

Myongnyang
Links: https://www.zhihu.com/question/20650946/answer/15745831
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Job 2: Use file format, read in length and width, calculate area and perimeter of rectangle
#-*-Coding:utf-8-*-
# D:\python\test.py
length = Int (raw_input ("Enter length:"))
width = Int (raw_input ("Enter width:"))
The area of the print U "Rectangle is:", length*width
The perimeter of the print U "Rectangle is:", (Length+width)

Execution Result:
C:\users\***>python d:\python\test.py
Enter Length:8
Enter Width:5
The area of the rectangle is: 40
The circumference of the rectangle is: 26

Review
>>> str=raw_input ("Please enter character:")
Please enter a character: I'm in a good mood today.
>>> Print str
I'm in a good mood today.
>>> Num=int (raw_input ("Please enter Number:"))
Please enter Number: 100
>>> Print num*99
9900

Python indent recommended 2 or 4 spaces, tab not recommended

Control Flow--if Statement
>>> a=2
>>> b=1
>>> if A>b:
... print "A greater than B"
...
A is bigger than B
>>>
>>> a=1
>>> b=2
>>> if A>b:
... print "A>b"
...
>>> str1= ' a '
>>> str2= ' B '
>>> if STR1>STR2:
... print "STR1>STR2"
.. else:
... print "STR1<STR2"
...
Str1<str2
>>> a=1
>>> b= "a"
>>> if A>b:
... print "A>b"
.. else:
... print "A<b"
...
A<b
Anscii

There are no switch statements in Python

Take Xiao Ming's test results as input values, and to judge their achievements
>90 print "Xiao Ming's score is excellent"
80<grade<90 print "Xiao Ming's results are good"

#-*-Coding:utf-8-*-
# D:\python\test.py
Grade=int (Raw_input ("Enter xiaoming ' s Grade:"))
If grade>=90:
Print U "Xiao Ming's score is excellent"
Elif grade>=80 and grade<90:
Print U "Xiao Ming's score is good"
Elif grade>=70 and grade<80:
Print U "Xiao Ming's results are in the middle"
Elif grade>=60 and grade<70:
Print U "Xiao Ming's grades are passing"
Else
Print U "Xiao Ming's grades fail"

Execution Result:
C:\users\***>python d:\python\test.py
Enter Xiaoming ' s grade:91
Xiao Ming's grades are excellent

C:\users\***>python d:\python\test.py
Enter Xiaoming ' s grade:88
Xiao Ming's grades are good

C:\users\***>python d:\python\test.py
Enter Xiaoming ' s grade:75
Xiao Ming's grades are in

C:\users\***>python d:\python\test.py
Enter Xiaoming ' s grade:66
Xiao Ming's grades are passing.

C:\users\***>python d:\python\test.py
Enter Xiaoming ' s grade:50
Xiao Ming failed his grades.

Job 1: Enter a character as password, correct: login succeeded; error, Login failed.

Control Flow--while Statement

Dead Loop, CTRL + C interrupt
a=2
B=1
While A>=b:
Print "A>b"

a=2
B=1
While a>=b: #表达式为 True, the loop body is executed, otherwise false, the loop body is not executed
Print "A>b"
A=a-1
Print U "The value of a in the current loop:", a

Execution Result:
C:\users\***>python d:\python\test.py
A>b
Value of a in the current loop: 1
A>b
Value of a in the current loop: 0

Guessing games-only three guesses
equals: Guess right, quit the game
Greater than: guess big
Less: guess small.
Guess three wrong exit loops

#-*-Coding:utf-8-*-
# D:\python\test.py
Num=25
Count=3
While not count = = 0:
Guess=int (Raw_input ("Please enter guess:"))
If Guess==num:
Print U "Congratulations on your guess, the game is over!" "
Break
Elif Guess>num:
Print U "input number is too big, guess again"
Count-= 1
Else
Print U "input number is small, guess again"
Count-= 1
Else
Print U "can only guess three times, no guess at once, the game is over!" "
Print U "program finished! "

Execution Result:
C:\users\***>python d:\python\test.py
Please enter guess:33
Input number is too big, guess again
Please enter guess:22
Input number is small, guess again
Please enter guess:21
Input number is small, guess again
Can only guess three times, the game is over!
Program execution Complete!

C:\users\***>python d:\python\test.py
Please enter guess:33
Input number is too big, guess again
Please enter guess:42
Input number is too big, guess again
Please enter guess:25
Congratulations, you guessed it, the game is over!
Program execution Complete!

C:\users\***>python d:\python\test.py
Please enter guess:25
Congratulations, you guessed it, the game is over!
Program execution Complete!

0603 Python Basics 02

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.