Python variable input and output

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Here are some basic Python basics for Beginners
#变量及类型
Objective:
To make better use of memory space and more efficient management of memory, variables are of different types
Type:
Number: int integer type; long integer;
Float floating-point number type; compelx plural;
Boolean type: True;flase;
String: Strings;
List of lists;
Tuple Ganso;
Dictionary dictionary;
How to identify the type:
In Python, as long as a variable is defined, and it has data, then its type is determined, without the developer's initiative to explain its type, the system will automatically identify
You can use the type (the name of the variable) to see the type of the variable

#标识符
What is an identifier?
Some of the symbols and name identifiers that developers customize in a program are defined by themselves, such as variable names, function names, and so on.
Naming rules for identifiers:
Identifiers consist of letters, underscores, and numbers, and numbers do not start
#关键字
What is a keyword?
Python some identifiers with special functions, this is called the keyword
The keyword, which python has already used, does not allow the developer to define the identifier of the same name as the keyword.
The current keyword in python:
And as Assert break class continue Def del
Elif else except exec finally for from global
If in import is a lambda not or pass
Print raise return try while with yield
commands for viewing a keyword in python
Import keyword
Keyword.kwlist
#格式化输出
Example:
Age = 18
Name = "Xiaohua"
Print ("My name is%s, age is%d"% (name, ages))
In the program, you see a% such operator, which is the formatted output in Python.
Commonly used format symbols:
Format symbol Conversions
%c character
%s string
%d signed decimal integers
%u unsigned decimal integer
%o Eight-binary integers
%x hexadecimal integer (lowercase letter 0x)
%x hexadecimal integer (capital letter 0X)
%f floating Point
%e Scientific counting method (lowercase ' e ')
%E Scientific notation (uppercase "E")
Shorthand for%g%f and%e
Shorthand for%G%f and%e
Line break output
At the time of the output, if there is \ n, then the contents of \ n will be displayed on another line
#输入
Python2:
The parentheses in raw_input () enclose a hint that is used to give the user a simple hint before getting the data
Raw_input () is stored in the variable to the right of the equal sign after getting the data from the keyboard
Raw_input () treats any value entered by the user as a string
Input () accepts the expression input and assigns the result of the expression to the variable to the left of the equal sign, which is dangerous and should be used with caution.
Python3:
There is no raw_input () function, only input () and input in Python3 is the same as the Raw_input () function in Python2
#运算符
Python supports the following types of operators:
1. Arithmetic operators:
+ Plus
Two objects added a + B output result 30
-Minus
Get negative numbers or a number minus another number A-B output 10
* Multiply
Two number multiplied or returns a string that is repeated several times a * b output result 200
/except
B/A Output Results 2
Take the Divide
Returns the integer portion of the quotient 9//2 output result 4, 9.0//2.0 output 4.0
% take-up
Returns the remainder of division B% A output result 0
* * Index
A**b is 10 of 20, output 100000000000000000000
Attention:
When blending operations, the order of precedence is: * * higher than */%//above +--to avoid ambiguity, it is recommended to use () to handle operator precedence.
Also, when different types of numbers are mixed, integers are converted to floating-point arithmetic
2. Assignment operators
= Assignment operator assigns the result to the right of the = sign to the left variable, such as Num=1 +2*3, and the result Num has a value of 7
3. Compound assignment Operator:
+ = addition Assignment operator C + = A is equivalent to C = C + A
-= Subtraction Assignment operator C-= A is equivalent to C = c-a
*= multiplication assignment operator C *= A is equivalent to C = c * A
/= Division assignment operator C/= A is equivalent to C = c/a
%= modulo assignment operator C%= A is equivalent to C = c% A
**= Power assignment operator C **= A is equivalent to C = c * * A
= Take divisible assignment operator C//= A is equivalent to C = c//A
#python中常见的数据转换
int (x [, Base])
Convert x to an integer
Float (x)
Convert x to a floating-point number
Complex (real [, Imag]) creates a complex number, real is the real part, and Imag is the imaginary part
STR (x)
Convert an object x to a string
REPR (x)
Convert an object x to an expression string
eval (str)
Used to calculate a valid Python expression in a string and return an object, note that this is dangerous, do not leave this function interface to untrusted users
Tuple (s)
Converting a sequence s to a tuple
List (s)
Convert the sequence s to a list
Chr (x)
Converts an integer to a Unicode character
Ord (x)
Converts a character to its ASCII integer value
Hex (x)
Converts an integer to a hexadecimal string
Oct (x)
Converts an integer to an octal string
Bin (x)
Converts an integer to a binary string

Python variable input and output

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.