Python's path -02 python basics

Source: Internet
Author: User
Tags arithmetic

Variable Declaration and assignment

Declaring variables: name = "Alex Li"

The code above declares a variable named: name, and the value of the variable name is: "Lanhan"

#! Author:lanhan
Name = "Lanhan"
name2 = Name
Print ("My name is", name)

Name = "Wang"
Print (name,name2)

Rules for variable definitions:

the variable name can only be any combination of letters, numbers, or underscores

The first character of a variable name cannot be a number

The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

2.2 Constant Declaration and assignment

constant Declaration and assignment: Age = "27"

Constant declarations are capitalized, constants should not be modified, but can be changed

2.3 Character encoding and binary

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256-1, so the ASCII code can only represent 255 letters and symbols.

About Chinese

in order to deal with Chinese characters, the programmer designed a simplified Chinese GB2312 and Big5 for Traditional Chinese

apparently The ASCII code cannot represent all the words and symbols in the world, so a new encoding that can represent all the characters and symbols is needed, namely: Unicode

Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...

Therefore,when the Python interpreter loads the code in the. py file, the content is encoded (default Ascill)

The history of character encoding is as follows:

Note:2.7 does not support Chinese, need to add #-*-Coding:utf-8-*-and 3.3 direct support

Character encoding and transcoding

Note: 1. The default encoding in Python2 is ASCII, the default is Unicode in Python3

    1. in the Py3 encode, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string

Example 1:

#! Author:lanhan
msg = "I love Tiananmen Square"
Print (msg)
Print (Msg.encode (encoding="Utf-8"))
Print (Msg.encode (encoding="Utf-8"). Decode (encoding="Utf-8"))

Example 2:

#! Author:lanhan
#-*-Coding:utf-8-*-
Import sys
Print (sys.getdefaultencoding ())
s = "Hello"
S_to_unicode = S.decode ("Utf-8")
Print (S_to_unicode)
‘‘‘
S_TO_GBK = S.decode ("Utf-8"). Encoding ("GBK")
Print (S_TO_GBK)
Print ("Hello")
‘‘‘

Note: The error here is normal, because #-*-coding:utf-8-*- defines the file encoding, not the program encoding, so the program default encoding or Unicode

Example 3:

2.4 Notes

    1. Single-line comment:# commented content
    2. Multiline Comment:"" "Annotated Content" "" (3 single quotation marks are also OK)

Expansion:3 single quotes can be used to print input values

2.5 user Input

Input ()

extension 1: string concatenation " + variable +" in fixed format

extension 2: string concatenation by fixed format%s

PS: string is%s; integer%d; floating-point number%f

extension 3: String formatting stitching. Format

extension 4: Sequential format stitching. Format

#! Author:lanhan
Name = input ("What is Your Name:")
age = Int (input ("Age:")) ##### #强制将string类型转换成integer
Print (Type (age), type (str)) ###### #打印变量字符类型
info = ""
-----Info of%s-------
name:%s
age:%d
"% (Name,name,age)
#print (INFO1)

Info2 = "'
-----info of {_name}-------
Name:{_name}
Age:{_age}
". Format (_name=name,
_age=age)
#print (Info2)

Info3 = "'
-----info of {0}-------
NAME:{0}
Age:{1}
". Format (Name,age)
Print (INFO3)

extension 5: String Casting

extension 6: Password Dark text

#-*-Coding:utf-8-*-

Import Getpass

# Assign user-entered content to the name variable

PWD = Getpass.getpass ("Please enter password:")

# Print What you have entered

Print (PWD)

2.6 Python Run process

When the Python program runs, the result of the compilation is saved in the Pycodeobject in memory, and when the Python program finishes running, the Python interpreter writes Pycodeobject back to the PYc file.

when the Python program runs for the second time, the program will first look for the PYc file in the hard disk, if found, the direct comparison with the source of time, if more than the source time is directly called, otherwise it will be recompiled.

so we should be positioning ourselves like this. Pycodeobject and PYc files (pre-compiled bytecode files

), we say that the PYc file is actually a persistent way to save Pycodeobject

2.7 Data types

2.7.1 Digital

2.7.1.1 int(integral type)

on a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807

2.7.1.2 Long(L-integer)

with Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, we use a long integer value can not be infinite. since Python2.2, if an integer overflow occurs, Python automatically converts the integer data to a long integer , andpython3.x is called an integral type.

2.7.1.3 float(float type)

a floating-point number is used to process real numbers, which are numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol

2.7.1.4 complex(plural)

The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers.

Note: Small number pools exist in Python:-5 ~ 257

2.7.2 Boolean value

True or False

  1 or 0

2.7.3 String

"Hello World"

2.8 Data Operations

2.8.1 arithmetic Operations

2.8.2 Comparison Operations

2.8.3 Assignment Operation

2.8.4 Logical operation

2.8.5 member Operations

2.8.6 Identity Operation

2.8.7 bit arithmetic

The smallest unit that can be represented in a computer is a bits

The smallest unit that can be stored in a computer is a bits (bit)

8bit = byte (bytes)

1024byte = 1kbyte

1024kbyte = 1mbyte

1024MB = 1GB

1024GB = 1T

Example:

A = 60

b = 13

c = 0

c = A & B;

Print "line 1-value of C is", C

calculation :

128 64 32 16 8 4 2 1

A = 60 0 0 1 1 1 1 0 0

b = 13 0 0 0 0 1 1 0 1

and

----------------------------------------------------------

0 0 0 0 1 1 0 0 = 12

Or

0 0 1 1 1 1 0 1 = 61

---------------------------------------------------------------------

^

0 0 1 1 0 0 0 1 = 49

~a (195-256=-61)

1 1 0 0 0 0 1 1 = 195

A << 2 (shift left 1 bits, multiplied by 2)

60*2*2 =240

A >> 2 (shift right 1 bits, divided by 2)

60/2/2 =15

2.8.8 Operator Precedence

2.9 bytes Type

The most important new feature of Python 3 is probably a clearer distinction between text and binary data. Text is always Unicode, represented by the STR type, and binary data is represented by the bytes type. Python 3 does not mix str and bytes in any implicit way, which makes the distinction between them particularly clear. You cannot stitch strings and byte packets, search for strings in a byte packet (or vice versa), or pass a string into a function with a byte packet (or vice versa).

#! Author:lanhan
msg = "I love Tiananmen Square"
Print (msg)
Print (Msg.encode (encoding="Utf-8"))
Print (Msg.encode (encoding="Utf-8"). Decode (encoding="Utf-8"))

Note: The default encoding in Python3 is the system code for the default encoding in utf-8,python2

2.9.13-dollar operation

result = value 1 if condition Else value 2

if the condition is true:result = value 1
if the condition is false:result = value 2

2.9.2 in the system

2.9.2.1 binary

2.9.2.2 octal

2.9.2.3 Decimal

2.9.2.4 Hex

Hexadecimal vs. Decimal relations:

10:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

16:0 1 2 3 4 5 6 7 8 9 A B C D E F

Hexadecimal vs. Binary relations:

Binary conversion Hex:

Binary conversion to 16 binary method is to take four-in-one method, that is, from the decimal point of the binary points to the left (or to the right) every four bits (can not be made up four bits, the left (or rightmost) of the decimal point 0, the conversion)

16 Notation of the binary:

suffix: H Example: BH represents 16 binary 11

Prefix: 0x* Example:0X23 is the

Binary conversion Hex:

A point four, that is, a hexadecimal number is divided into four binary numbers, with four-bit binary by the right to add, and finally get the binary, the decimal point is still OK

Python's path -02 python basics

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.