Python Basics 1

Source: Internet
Author: User
Tags ord

Python command-line mode launches Python interactive mode

Exit () command line mode exits Python interactive mode

Python interactive mode: Enter a command to interpret the output immediately

Command-line mode: Run file, Python. py

Python files directly in the text editor to write code, there is no header file exists, the import file can be

Input, output input () print ()--"A=input (" Please enter Number: ") print (' string ', ' comma ', ' delimited ', ' space ') input() and print() is the most basic input and output below the command line

Python uses indentation to organize blocks of code, following the habit of using indents of 4 spaces . Each row is a statement that, when the statement ends with a colon : , the indented statement is treated as a block of code . The comment # begins with an arbitrary content, and the interpreter ignores the comment. tab (tab, used for its character), space (visible character)

Python programs are case-sensitive

Python can handle integers of any size, including, of course, negative integers, which are represented in the program in the same way as mathematically, for example:,,, 1 100 , and so on -8080 0 . Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is 0x represented by prefixes and 0-9,a-f, for example: 0xff00 ,, and 0xa5b4c3d2 so on. 1.23X109 is 1.23e9 , or 12.3e8 , 0.000012 can be written 1.2e-5 , and so on. Python has no size limit on integers and floating-point numbers.

A string is any text enclosed in single or double quotation marks " , such as ‘abc‘ , and "xyz" so on. Note that ‘‘ or "" itself is just a representation, not part of a string,

What if the inside of a string contains and contains both " ? Can be identified by an escape character \ , such as:‘I\‘m \"OK\"!‘。\n表示换行,\t表示制表符,字符\本身也要转义,所以\\表示的字符就是\

Python allows ‘‘‘字符串分行写所见即所得‘‘‘ a format that represents multiple lines of content with three single quotes on both sides

print(‘‘‘line1                                line1  

line2 输出
   line3‘‘‘)  line3

R ' string ' or R ' string ' will be written in front of the string in the R ' ' string ' r, all characters in the prompt string are displayed directly, no longer escaped. such as: R ' I \\\\ ' output me \\\\, otherwise ' i \\\\ ' output me \ \

Print (R ' ' hello,\n hello,\n

World ") Output world

In Python, you can directly use True , False represent a Boolean value (note case) For example, print (3>2) output True. Boolean values can be used and , or and not operations. True and True, False or false, not true.

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

Variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, a number _ , and cannot begin with a number . 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. This type of variable itself is called Dynamic language , which corresponds to static language. Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language.

In Python, constants are typically represented in all uppercase variable names, for example, PI = 3.14159265359, but in fact PI still a variable, and Python does not have any mechanism to guarantee that it PI will not be changed, so the use of all uppercase variable names to denote constants is only a customary usage. If you must change PI the value of the variable, no one can stop you.

Finally, explain why the division of integers is also accurate. In Python, there are two types of division, and a division is/,/除法计算结果是浮点数,即使是两个整数恰好整除,结果也是浮点数:

>>> 10 / 33.3333333333333335
>>> 9 / 33.0

There is also a division that is // called a floor divide, where the division of two integers is still an integer:

10 // 33

You are not mistaken, the whole number of floors apart // is always an integer, even if not endless. To do the exact division, you / can use it. Because // division takes only the integer part of the result, Python also provides a remainder operation that can be used to divide the remainder of two integers:

>>> 10 % 31

The result is always an integer, regardless of whether the integer // divides or takes the remainder, so the result of the integer operation is always accurate.

Strings and encodings

Generic, called in the operating system. The name and type of the encoding:

ASCII encoding is 1 bytes, and Unicode encoding is usually 2 bytes, in the spirit of saving, there is the conversion of Unicode encoding to "Variable length encoding" UTF-8 encoding. The UTF-8 encoding encodes a Unicode character into 1-6 bytes according to a different number size, the commonly used English letter is encoded in 1 bytes, the kanji is usually 3 bytes, and only the very uncommon characters are encoded into 4-6 bytes.

Now the computer system common character encoding working mode:

In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.

When editing with Notepad, the UTF-8 characters read from the file are converted to Unicode characters into memory, and when the edits are complete, the conversion of Unicode to UTF-8 is saved to the file.

Save to the hard disk or transfer, the need for smaller files, so converted to UTF-8.

Inside Python, a Unicode-encoded type called STR is used, either ASCII-encoded or UTF-8-encoded, called the bytes type. Note the difference between the type of Python and the specific encoding type, do not confuse, encoding is the encoding, type is the type, one is the bottom, the other is the surface. Bytes is only a type, and the specific underlying encoding format needs to be specified.


python bytes uses b a prefixed single or double quotation mark for data of type:

x = b‘ABC‘

A string of type Str is converted to a string of the corresponding bytes type : A Unicode representation of the str pass encode() method can be encoded as specified bytes , for example:

>>> ‘ABC‘.encode(‘ascii‘)                     (实际上英文使用的是ASCII编码)     或者  ‘ABC‘.encode(‘utf-8‘)      utf-8等同于UTF-8(一下都是)b‘ABC‘                                          一样的结果,因为在ASCII编码中和utf-8编码中,一个英文字母都占用一个字节>>> ‘中文‘.encode(‘utf-8‘)                    (实际上中文使用的是utf-8编码,一个汉字占用3个字节) 不能使用ASCII编码因为超出了ASCII规定的编码范围。b‘\xe4\xb8\xad\xe6\x96\x87‘                     bytes有6个字符,占用6个字节

turn bytes str it into, you need to use the decode() method :

>>> b‘ABC‘.decode(‘ascii‘)                       b‘ABC‘.decode(‘utf-8‘)  结果一样‘ABC‘>>> b‘\xe4\xb8\xad\xe6\x96\x87‘.decode(‘utf-8‘) 用什么加锁就用什么解锁喽~‘中文‘

bytes to STR, if bytes there are only a small number of invalid bytes, you errors=‘ignore‘ can pass in a byte that ignores the error:

>>> b‘\xe4\xb8\xad\xff‘.decode(‘utf-8‘, errors=‘ignore‘)‘中‘

We often encounter str and convert to and bytes from each other when manipulating strings. In order to avoid garbled problems, we should always adhere to the use of UTF-8 encoding str and bytes conversion . Both English and Chinese are selected using Utf-8,encode () and Decode ().

Setting the text editor to the Utf-8without BOM format ensures that the Python source code is UTF-8 encoded when it is saved, and that adding two lines of comments before the source file ensures that the read is UTF-8 encoded:

Because the Python source code is also a text file, so when your source code contains Chinese, it is important to specify that you save it as UTF-8 encoding when you save it. When the Python interpreter reads the source code, in order for it to be read by UTF-8 encoding, we usually write these two lines at the beginning of the file:

#!/usr/bin/env python3# -*- coding: utf-8 -*-

The first line of comments is to tell the Linux/os x system that this is a python executable, and the Windows system ignores this comment; the second line of comments is to tell the Python interpreter to read the source code according to UTF-8 encoding, otherwise The Chinese output you write in the source code may be garbled. Affirming that UTF-8 encoding does not mean that your .py file is UTF-8 encoded, you must make sure that the text editor is using UTF-8 without BOM encoding.

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:

>>> ord(‘A‘)65>>> ord(‘中‘)20013>>> chr(66)‘B‘>>> chr(25991)‘文‘

If you know the integer encoding of a character, you can also write it in hexadecimal str :

>>> ‘\u4e2d\u6587‘‘中文‘

To calculate str how many characters are included, you can use a len() function:

>>> len(‘ABC‘)3>>> len(‘中文‘)2

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

>>> len(b‘ABC‘)3>>> len(b‘\xe4\xb8\xad\xe6\x96\x87‘)6>>> len(‘中文‘.encode(‘utf-8‘))6
In Python, the format used is consistent with the C language, and the % % operator is used to format the string. Inside the string, the representation is replaced by a string, which %s %d is replaced with an integer, there are several %? placeholders, followed by a number of variables or values, the order to correspond well. If there is only one %? , the parentheses can be omitted.
Print (' hi,%s '%' liMei ') red % in front of the content to be output, inside the%s, \%d,%. is a placeholder, and the value used for the replacement is after the red % number

' hi,%s, you have%d dollars ' % (' Li Mei ', 7000) , the value behind the red percent sign is enclosed in parentheses and the order corresponds well.

Common placeholders are:

placeholder Replace content
%d Integer
%f Floating point number
%s String
%x hexadecimal integer
where formatted integers and floating-point numbers can also specify whether to complement 0 and the number of digits of integers and decimals:

Print ('%2d-%02d '% (3, 1))
Print ('%.2f '% 3.1415926)
Print ('%4.2f '% 13.1415926)



Format ()

Another way to format a string is to use the string format() method, which in turn replaces the placeholder in the string with the passed-in argument, {0} {1} but this is a much more troublesome way to write than%:

>>> ‘Hello, {0}, 成绩提升了 {1:.1f}%‘.format(‘小明‘, 17.125)‘Hello, 小明, 成绩提升了 17.1%‘

The Python 3 string uses Unicode and supports multiple languages directly.

You str bytes need to specify the encoding when and when you convert to each other. The most commonly used encoding is UTF-8 . Python, of course, also supports other encodings, such as encoding Unicode into GB2312 :

>>> ‘中文‘.encode(‘gb2312‘)b‘\xd6\xd0\xce\xc4‘

But this is a way of asking for trouble, and if there are no special business requirements, remember to use UTF-8 coding only.

Python Basics 1

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: 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.