Python Learning-data types, operators, conditional statements

Source: Internet
Author: User
Tags arithmetic operators

1.python encoding Format
python2:ASCIIpython3:Unicode

ASCII encoding:
1 bytes (bytes) = 8 bits (bit) an English character takes one byte,

Unicode:
Usually a character is stored in two bytes, either English or Chinese, and the English is 0 in front. If you want to use a very remote character, you need 4 bytes.

Utf-8:
Automatically determine the character type, the commonly used English letter is encoded into 1 bytes, the Chinese character is usually 3 bytes, only the very uncommon characters will be encoded into 4-6 bytes.

To view keyword Help:
Help (keyword, function name)
will be listed using formats, return values, related built-in functions, etc.

2. Common data types

(1) integral type (int)
The Python2 has a long integer type.
There are no long integers in the python3, only integral types.

(2) Float type (float)
12.0 0.12 1.2e3 (Value of 1.2e-3) (value 0.0012)

(3) Complex Type (complex)
2j+3 the real part, the imaginary part of its type will automatically be converted to floating-point type

(4) string (str)
Strings need to be in single or double quotes, such as assigning a string to a variable astr: astr= ' ssssddd ' or astr= ' sssddd '

(5) Boolean data type (BOOL)
He has only two values: True,false

To determine a numeric type function: Type ()

Cast: Int (), float (), str ()

Delete variable: del variable name

3. Simple input and output

1. Enter
Python2:
Input ()---> only accepts numeric type input
Raw_input ()---> Accepts string types.
Python3:
Input ()---> Numeric types, strings are received, but all are stored as string types

How does python implement multiple values in one line?
Split function Split ()

If 33,44,55 is copied to a,b,c three variables at one time;
A,b,c=input (). Split (', ')

2. Output
Print ()
Output placeholder:%s string,%dxxx,%f floating-point

>>> name=‘linux‘>>> age=27>>> grade=88.5>>> print("%的年龄是 %d,成绩是%f" %(name,age,grade))linux的年龄是27,成绩是88.500000

(1) Formatted output

>>> idcard=9527>>> salary=9999.9527003>>> print("编号%d的人员的薪水是%.2f" %(idcard,salary))编号9527的人员的薪水是9999.95

Keep 2 digits after the decimal point

>>>name=‘黄山大傻‘>>> idcard=9527>>> print("%s的编号是%.8d" %(naem,idcard))黄山大傻的编号是00009527

Integer reserved 8 bits, not enough to add 0 in front

4. Operators and expressions

The following assumptions are a=10,b=20
Arithmetic operators:

operator Description Example
+ Add-Two objects added A + B output result 30
- Minus-get negative numbers or one number minus the other -B Output Result-10
* Multiply by two number or return a string that is repeated several times A * b output result 200
/ Except-X divided by Y B/A Output Results 2
% Residual-Returns the remainder of the division B% A output result 0
** Power-Returns the Y power of X A**b is 10 of 20, output 100000000000000000000
/ Rounding-Returns the integer portion of the quotient (rounded down) 9//2 output result 4, 9.0//2.0 output 4.0

Comparison operators:

operator Description Example
== Equals-compares objects for equality (A = = B) returns FALSE.
!= Not equal-compares two objects for unequal (A! = B) returns TRUE.
<> Not equal-compares two objects for unequal (a <> B) returns True. This operator is similar to! =.
> Greater than-returns whether x is greater than Y (A > B) returns FALSE.
< Less-Returns whether x is less than Y. (A < B) returns TRUE.
>= Greater than or equal-returns whether X is greater than or equal to Y. (a >= B) returns FALSE.
<= Less than or equal-returns whether X is less than or equal to Y. (a <= B) returns True.

All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false.

Assignment operators:

operator Description Example
= Simple assignment operator c = A + B assigns the result of the operation of A + B to c
+= 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 the divisible assignment operator C//= A is equivalent to C = c//A
5. Conditional statements

The judgment condition of the IF statement can be expressed by > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than or equal).

(1) Single Branch

if 判断条件:    执行语句

(2) Dual Branch

if 判断条件:    执行语句1……else:    执行语句2……

(3) Multi-branch

if 判断条件1:    执行语句1……elif 判断条件2:    执行语句2……elif 判断条件3:    执行语句3……else:    执行语句4……

(4) Python (indirect) three-mesh operator:

Three-mesh operator syntax format for other languages:

判断条件(返回布尔值)?为真时的结果:为假时的结果

But there is no syntax in Python, but there is a syntax like this:

"变量1" if a>b else "变量2"

The condition is determined to be true, return the preceding variable 1, return the following variable for false 2
A simple formula can also be used in the position of a variable, for example:

a-b if a>b else a+b

The three-mesh operator in Python is designed to get a result, but in fact it is not necessarily the return of the result, we can make a simple variable assignment
For example:

c=a-b if a>b else a+b

Python Learning-data types, operators, conditional statements

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.