According to Liao Xuefeng python3 tutorial----python Study the next day

Source: Internet
Author: User
Tags mathematical constants ord

Input and output

Using print () to add a string to the parentheses, you can output the specified character eg to the screen:

>>> print (' Hello, World ')

The print () function can also accept multiple strings, separated by a "," comma between the strings, eg:

>>> print (' The quick brown fox ', ' Jumps over ', ' the Lazy Dog ') The "quick brown fox jumps over the" the Lazy Dog


print()Each string will be printed sequentially, and a comma "," will output a space, so the output string is spelled like this: 650) this.width=650; "Src="/e/u261/themes/default/images/spacer.gif " Style= "Background:url ("/e/u261/lang/zh-cn/images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= " Spacer.gif "/>

Output

Python provides one that allows the input() user to enter a string and store it in a variable. For example, enter the user's name:

>>>name = input () xiaoming

When you enter name = input () and press ENTER, the python interactive command line is waiting for you to enter. At this point, you can enter any character, and then continue to enter then there will be no

The prompt. Python interactive command-line mode is back to >>>, when you can

>>>name ' Xiaoming '

In addition to this approach, you can also use the print () function to output

>>>print (' name= ', name) name=xiaoming

The input () function also provides a hint that you can use to prompt the user for input. eg

>>> name= input (' Pieas input your Name: ') pieas input your name:

This format provides a more user-friendly interaction when writing a. py script.

A question about the output format

>>> name=input () anne>>> print (' Hello, ', name, ', that's a beautiful mame ') Hello, Anne, that's a Beautifu L MAME

There is a comma-removed method before the comma behind Anne

>>> print (' Hello, ', '%s, '%name, ' that's a beautiful name ') Hello, Anne, that's a beautiful name>>> print ( ' Hello, ' +name+ ', that's a beautiful name ') Hello,anne,that is a beautiful name>>> print (' Hello,%s,this is a Beauti Ful name '% (name)) Hello,anne,this is a beautiful name>>> print (' Hello,%s,this is a beautiful name '%name) Hello, Anne,this is a beautiful name

Data type

Integer

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 , 0xa5b4c3d2 , etc.

Floating point number

Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are exactly equal.

Floating-point numbers can be written in mathematical notation, such as,, 1.23 3.14 , and -9.01 so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9 , or 12.3e8 , 0.000012 can be written 1.2e-5 , and so on.


Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.


String

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, so the string is ‘abc‘ only a , b c this 3 characters. If it is also a character, it can be "" enclosed, such as the character that contains it,,, the space, the "I‘m OK" I m O K 6 characters.

What if the inside of a string " contains and contains both? Can be identified by \ an escape character, such as:

  ‘I\‘m \"OK\"!‘

>>> print (' i\ ' m\ "Ok\" ') I ' m "OK"

Escape character \

An escape character \ can escape many characters, such as a \n newline, a \t tab, and the character \ itself, so \\ the character represented is\

>>> print (' i\ ' m\ "Ok\" ') I ' m "OK"
>>> print (' i\ ' M,ok ') I ' M,ok
>>> print (' i\ ' m learning\npython ') I ' m Learningpython
>>> print (' \\\n\\ ') \

If there is a lot of line wrapping inside \n The string, writing in a row is not good to read, ‘‘‘...‘‘‘ in order to simplify, Python allows a format to represent multiple lines of content, you can try it yourself:

>>> print ("' Line1 ... line2 ... line3") line1line2line3


Boolean type

Boolean values are exactly the same as Boolean algebra, with a Boolean value of only two values, either, or, True False True False in Python, you can directly use True , represent a False boolean value (note case), or you can calculate it by Boolean:

>>> truetrue>>> falsefalse>>> 3 > 2true>>> 3 > 5False




Boolean values can be used and , or and not operations.

  andOperations are and operations, and only all are True , the result of the and operation isTrue

  orAn operation is or an operation, as long as one of them is True , the result of the or operation isTrue

  notThe operation is a non-operation, it is a monocular operator, turns True into a False FalseTrue


Null value

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.


Variable

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 different types of variables eg:

>>> a=123>>> Print (a) 123>>> a= ' abc ' >>> print (a) ABC



Constant

So-called constants are immutable variables, such as the usual mathematical constants π is a constant. In Python, constants are typically represented in all uppercase variable names.


In Python, there are two types of division, and a division is/

>>> 10/33.3333333333333335

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

>>> 10//33

Python also provides a remainder operation%, which can be used to divide the remainder of two integers

>>> 10%31


Character encoding

Because a computer can only handle numbers, if you are working with text, you must convert the text to a number before processing it. The oldest computer was designed with 8 bits (bit) as a byte (byte), so a single word energy-saving representation of the largest integer is 255 (binary 11111111 = decimal 255), if you want to represent a larger integer, you must use more bytes. For example, two bytes can represent the largest integer is 65535 , 4 bytes can represent the largest integer is 4294967295 .

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 (' B ') 66>>> chr ($) ' $ ' >>> chr (20013) ' in '

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

>>> ' \u4e2d\u6587 ' Chinese
>>> x= ' abc ' >>> print (x) abc>>> x=b ' abc ' >>> print (x) b ' ABC '

Be aware of the distinction ‘ABC‘ and the b‘ABC‘ former is str that although the content is displayed in the same way as the former, bytes each character occupies only one byte .

The str pass method, expressed in Unicode, encode() can be encoded bytes as specified , for example:

>>> ' abc '. Encode (' ASCII ') b ' ABC '
>>> ' Chinese '. Encode (' utf-8 ') B ' \xe4\xb8\xad\xe6\x96\x87 '

In bytes , the bytes that cannot be displayed as ASCII characters are \x## displayed.


If we read the byte stream from the network or disk, then the data read is bytes to turn bytes into STR, you need to use the decode () method

>>> b ' abc '. DECODE (' ASCII ') ' ABC '
>>> b ' \xe4\xb8\xad\xe6\x96\x87 ' decode (' utf-8 ') ' Chinese '


This article is from the "Creative Pilgrim" blog, so be sure to keep this source http://dearch.blog.51cto.com/10423918/1752687

According to Liao Xuefeng python3 tutorial----python Study the next day

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.