Python Learning Notes (ii) Python basics

Source: Internet
Author: User

#The statement that begins with Python is a comment.

When the Python statement ends with a:, the downward indented statement is treated as a block of code. (The Python contract indentation is 4 spaces ) In addition, the Python statement is case-sensitive, if the wrong case is written, the program will directly error.

Data types and variables:

  In Python, there are several types of data that can be processed directly: integers, floating-point numbers, strings, and Boolean values.

Integers: Computers because of the use of binary, it is sometimes more convenient to use hexadecimal notation for integers, hexadecimal 0x prefixes and 0-9,a-f, for example: 0xff00 , 0xa5b4c3d2 , and so on.

Floating point number: For very large or very small floating-point numbers, must be used in scientific notation, the 10 with e substitution, 1.23x109 is 1.23e9 , or 12.3e8 , 0.000012 can be written 1.2e-5 , and so on.

String: The string is ‘ any text enclosed in single or double quotation marks " , the difference between ' and ' is stated in (a), and here is a little supplement, if the string contains ‘ and contains " can be identified by an escape character \ ,

For example: The ‘I\‘m \"OK\"!‘ string content of the expression is: I‘m "OK"!; In addition, the escape character \ can escape many characters, such as a \n newline, a \t tab, the character \ itself is escaped, so \\ the character is\。

Python also allows r‘‘ the use ‘‘ of strings that represent internal literals that are not escaped by default. If there is a lot of line wrapping inside the string, it is not good to read in a single line, and in \n order to simplify, Python allows ‘‘‘...‘‘‘ the format to represent multiple lines of content.

Boolean value: A Boolean value of True only False two values. Boolean values can be used and , or and operations are not represented with, or, and not.

NULL: The null value is a special value in Python, denoted by None . Nonecannot be understood as 0 , because 0 it is meaningful, and None is a special null value.

Variable: In a computer program, a variable can be not only a number, but also any data type. In Python, the equals sign = is an assignment statement that assigns any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types.

Constants: In Python, constants are typically represented in all uppercase variable names.

Strings and encodings:

  Character encoding: ASCII encoding: 127 characters are encoded into the computer, that is, uppercase and lowercase letters, numbers, and some symbols. ASCII encoding is a byte.

Unicode:unicode unifies all languages into a set of encodings, but the most common is to use two bytes to represent a character (4 bytes if you want to use very remote characters).

UTF-8: Commonly used English letters are encoded in 1 bytes, Chinese characters are usually 3 bytes, only very uncommon characters will be encoded into 4-6 bytes. ASCII encoding can actually be seen as part of the UTF-8 encoding.

Python string: In the latest version of Python 3, strings are encoded in Unicode, meaning that Python strings support multiple languages;

For the encoding of a single character, Python provides an ord() integer representation of the function to get the character, and the chr() function converts the encoding to the corresponding character;

Because the Python string type is str , in memory, in Unicode, one character corresponds to a number of bytes. If you want to transfer on a network, or save to disk, you need to turn it str into bytes bytes . Python bytes represents a type of data in b single or double quotation marks with a prefix.

len()The function calculates the str number of characters, and if bytes so, the len() function computes the number of bytes.

Formatting: In Python, the format used is consistent with the C language and is % implemented. % must be escaped in the string, expressed in percent. Cases:

>>> ‘Hi, %s, you have $%d.‘ % (‘Michael‘, 1000000)‘Hi, Michael, you have $1000000.‘



Python Learning Notes (ii) 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.