Input and output, data type, variable details in python

Source: Internet
Author: User

This article describes the input output, data type, and variable details in Python

Python input output data type variable

Input/Output

    • Print () is a function in Python3

>>>print (' Hello World ')  #print注意print前面不要有任何空格

    • Print output multiple characters, the middle with a comma connection, the last output time to replace the space

    • name = input ()
      When the interactive row pops up, the input content will be stored in the name variable

Python Basics

Any programming language has its own syntax, and the compiler or interpreter is responsible for translating the syntax code into machine code and then letting the CPU execute it. Python is not a compiled type and is interpreted.
Python is mainly indented in the way

A=100if a >=0:    print (a) Else:    print (-a)

When the statement ends with a colon:, the indented statement is treated as a block of code.
I think that's the exact logical part.
Indentation has pros and cons. The advantage is that you are forced to write the formatted code, but there is no rule that indents are a few spaces or tabs. In accordance with established management, you should always stick to the indentation of 4 spaces.

Online about tab and 4 space argument too much, this specific or look at the company is the demand god what kind of

The downside of indentation is that the "copy-paste" function fails, which is the most pit-daddy place. When you refactor the code, the pasted past code must re-check that the indentation is correct. In addition, it is difficult for the IDE to format Python code just like formatted Java code.
Python program is case sensitive, if the case is wrong, the program will error

Python uses indentation to organize blocks of code, so be sure to follow the customary conventions and stick to the indentation of 4 spaces.

Note: You need to set the tab of all text editors to 4 spaces

Data type

A computer is a machine that can do mathematical calculations as the name implies, so a computer program can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly:

    • Integer
      Python can handle integers of any size, positive integers, negative integers such as 1000,-1000,0
      The computer uses binary, it is convenient to use hexadecimal to denote integers, 0x

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

You know, the floating-point numbers are like this.

Floating-point numbers can be written in mathematical notation, such as 1.23,3.14,-9.01, and 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 differently, integers are always accurate, and floating-point arithmetic has rounding errors

    • String
      strings are arbitrary text enclosed in single quotes ' or double quotes, such as ' abc ', ' XYZ ', and so on. Note that the ' or ' itself is only a representation, not a part of the string, so the string ' abc ' only a,b,c these 3 characters. If ' itself is also a character, then you can use "", such as "I m OK" contains the characters are I, ', M, space, o,k these 6 characters.

What if the inside of a string contains both ' and contains '? You can use the escape character \ To identify, for example:

' I\ ' m "OK"! '
This is the end of the ", after testing" is also possible

The most that is used in an escape character is \ n, newline
\ represents the character \
Python also lets you use R ' to indicate that the string inside of ' is not escaped by default

>>>print (R ' \\\t\\ ')

\\t\

There are a lot of line breaks inside the string, and if it's not easy to read in a row, there's a "..." in Python.

Print (' Line1line2line3 ')

Here we need to distinguish between multiple lines of comments
"""
Multi-line comments
Multi-line comments
Multi-line comments
"""

Multi-line annotations There is another case
'''
'''
This is more commonly used

    • Boolean value
      Boolean value only True and false, note case
      Boolean values can also be calculated by And,or,not

    • Null value
      None, is a special value, not 0, because 0 is meaningful

Python's data types also include lists, dictionaries, and so on, and also allows you to create custom data types

Variable

In a computer, a variable can be a number, or it can be any data type

Variables are represented in the program by a variable name, and the variable name must be a combination of uppercase and lowercase English, numeric, and _, and cannot begin with a number

In Python, equals = is an assignment statement that can assign any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types, for example:

A = 123 # A is an integer print (a) a = ' ABC ' # A becomes a string print (a)

The variable type itself is not fixed language is called Dynamic language, Java, C and other variables of the type are fixed, such as int A;

Dynamic language More flexible

The representation of a variable in computer memory:
such as A= ' ABC '
The Python interpreter does two things.

    1. An ' ABC ' string was created in memory;

    2. Create a variable named a in memory and point it to ' ABC '
      You can also assign a variable A to another variable B, which actually points the variable B to the data pointed to by the variable A.

Constant

Constants cannot be made into quantities, such as π, in Python, usually with all uppercase variable names representing constants

PI = 3.1415926

In fact pi is still a variable, Python does not have any mechanism to ensure that PI will not be changed, so, using all uppercase variable names to represent constants is only a customary usage, if you must change the value of the variable pi, no one can stop you

There are two types of division in Python, the difference between Python2

    1. The division calculates the floating-point number.

    2. The division is calculated as an integer//

Python2 are evenly divisible unless you change the number of operations into floating-point numbers.

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.