03-string + variable

Source: Internet
Author: User
Tags builtin

String
    • Escape character
    • Formatting
    • Built-in function (deferred)
Escape character
    • Use a special method to express a series of inconvenient content, such as enter key, line break, backspace
    • With the backslash character, once a backslash appears in the string, a few characters after the backslash will indicate that it is not the original meaning, and is escaped.
    • In a string, once a backslash appears, be more cautious, possibly by an escape character
    • There are different representations of different systems for line operations
      • Windows: \ n

Linux: \ r \ n

Formatting of strings
    • To print or fill a string in a certain format
    • Formatting consists of two methods
      • Use percent sign (%)

Using the Format function

Use percent formatting
    • In a string, a special meaning is represented by%, which means that the character is formatted
    • %d: You should put an integer here
    • %s: Indicates that a string should be placed here
In [12]:
"I Love%s"
# print below, print%s directly as a string of contents
Print (s)
I Love%s
In [13]:
Print ("I love%s"%"wangxiaojing")  
I Love Wangxiaojing
In [15]:
s%"wangxiaojing") 
I Love Wangxiaojing
In [18]:
"I am%d years old"
# Pay attention to the difference and result of the following two words
Print (s)
Print (s%)  
I am%d years oldi am years old
In [22]:
"I am%s, I am%d years old"
Print (s)
# Notice the reason for the error in the following expression
# If the string is made up of placeholders, then several placeholders must be replaced with several actual content, or one should not
#print (s% "Tulingxueyuan")
?
# If multiple placeholders appear, the corresponding content needs to be enclosed in parentheses.
Print (s% ())  
I am%s, I am%d years oldi am Tulingxueyuan, I am years old
Format function formatted string
    • Format directly with the Format function
    • It is recommended to use this method
    • On use, with {} and: instead of%, followed by format with parameter completion
In [25]:
"I Love {}". format ("lixiaojing") 
Print (s)
?
"Yes, I am {1} years old, I love {0} and I am {1} years". format (+) 
Print (s)
I love Lixiaojingyes, I am years old, I love Tulingxueyuan and I am all years old
None
    • It means there's nothing.
    • If the function does not return a value, you can return none
    • Used to occupy a position
    • Used to contact variable bindings
An expression
    • A line of code that is composed of one or several numbers or variables and operators
    • Typically returns a result operator
    • The process by which more than one value is changed to get a new value is called an operation.
    • The symbolic operator used for the operation
    • Operator Classifications:
      • Arithmetic operators
      • Comparison or relational operators
      • Assignment operators
      • logical operators
      • Bit arithmetic
      • Member operations
      • Identity operator
Arithmetic operators
    • Symbols for arithmetic operations
    • Python does not have a self-increment decrement operator
In [32]:
# +,-is identical to normal arithmetic operators
9+3-2   
Print (a)
# multiplication sign is replaced with an asterisk (*)
4
Print (a)
# Division sign with slash (/) instead
# in python2.x and python3.x, Division sign (/) results may be inconsistent and this is subject to the 3 series
9/4 
Print (a)
?
?
#% Take-rest operation
# Two digits should have quotient surplus
#% will only get the remainder
4
Print (a)
?
#//Indicates a fetch operation, also called a floor in addition to
4
Print (a)
?
# * * Indicates a power operation
4
Print (a)
?
3
Print (a)
10362.2512656127
Comparison operators
    • operator to compare two variables or values
    • The result of the comparison is a Boolean value, which is True/false
In [35]:
# = =, equals number
4
# The following statement executes the book order is
# 1, calculate a = = 80
# 2. Put the results in B
80
Print (b)
?
?
# = does not equal
8)
?
# > Greater than
# < less than
# >= greater than or equal
# <= less than equals
Falsetrue
Assignment operator in [37]:
# =, assignment
0
4
?
# + =, is the abbreviation,
0
# a = abbreviation for a + 7
Print (a)
?
?
# All mathematical operators have abbreviated forms
#-=,x=,/=,//=,%=, **=, all abbreviated forms
7
logical operators
    • Symbols for the calculation of Boolean values
    • and Logic and
    • or logical OR
    • Not logical non-
    • There is no XOR or operation of logical operations in Python
    • Operation rules:
      • And as multiplication, or as addition,
      • True as 1, false as 0
      • Then the logical operation can be converted into an integer mathematical operation.
      • The final result is false if 0, otherwise true
    • Short-circuit problem of logic operation
      • A logical operation, calculated according to the Order of operations, is no longer calculated, and is returned directly once it is able to determine the future value of the entire equation.
In [40]:
# logical operator case
True
False
True
?
# The following equation is equivalent to d = 1 * 0 + 1
C
Print (D)
?
?
A
Print (D)
Truetrue
In [43]:
# short-circuit case of logical operation
?
# The following logical expression, the value of a must be true, the entire expression is not evaluated down when running to or
Xxxxxxxxxxx
?
# The following expression, if XXX contains an assignment expression, the result is difficult to anticipate
# code example (pseudo code)
0
or (b=6 
# Assume the above expression if there is no syntax error
# then the final value of B should be 0 instead of 9.
Print (b)
Print (a)
  "<ipython-input-43-bb4253784911>"9    a = A or (b=9) and 6               ^syntaxerror: Invalid syntax 
Member Operation symbols
    • Used to detect whether a variable is a member of another variable
    • Inch
    • Not in
In [47]:
# case
L = [1,2,3,4,5]    
7
?
L
Print (b)
?
4
Print (L)
?
Print (L)
Falsetruefalse
Identity operations
    • is: Used to detect whether two variables are the same variable
      • Grammar is the var1 is var2
    • is not: Two variables are not of the same variable
In [50]:
9
9
b
?
"I Love Wangxiaojing"
"I Love Wangxiaojing"
b
TrueFalse
Precedence Issues for operators
    • Always remember that parentheses have the highest precedence
    • Priority table

        **  指数 (最高优先级)  ~ + -   按位翻转, 一元加号和减号 (最后两个的方法名为 [email protected] 和 [email protected])  * / % //    乘,除,取模和取整除  + - 加法减法  >> <<   右移,左移运算符  &   位 ‘AND‘  ^ | 位运算符  <= < > >=   比较运算符  <> == !=    等于运算符  = %= /= //= -= += *= **=    赋值运算符  is is not   身份运算符  in not in   成员运算符  not or and  逻辑运算符
Program Structure?
    • Order
    • Branch
    • Cycle

03-string + variable

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.