Python basic syntax

Source: Internet
Author: User
Tags bitwise terminates

    • Variable
    • Number Type
    • Operator
    • Process Control
    • While loop
    • Dead loop
    • Continue vs Break
    • While else
Variable (varibles)

The role of variables:

    • Variables are used to store information so that subsequent code calls
    • Tag Description Data
    • declaring variables

      name = "Jason"
    • Name: variable name
    • "Jason": Variable Value

Definition Specification for variables

    • A variable can only be a combination of letters, numbers, or underscores
    • The first character of a variable name cannot be a number
    • Variable assignments cannot have spaces such as:

      name age = 22
    • The following keywords cannot be declared as variable names

      ["and", "as", "assert", "break", "class", "continue", "del", "elif", "else", "except", "exec", "finally", "del", "for", "from", "gobal", "if", "import", "in", "is", "lambda", "not", "or", "pass", "print", "rasie", "return", "try", "while", "with", "yeild"]

Variable naming habits

    • Hump body

      AgeOfOldboy = 56NumberOfStudents = 80
    • Underline

      age_of _oldboy = 56
      Low mode of variable name
    • The variable is named Chinese, pinyin
    • Variable name too long
    • Variable nouns are not expressive

Constant

Constant, all uppercase letters.

AGE_OF_OLDBOY = 56
Number Type
    • int (integral type)

      在32位机器上,整数的位数为32位,取值范围为-2**31~2**32-1,即-2147483648~214748364
    • Long (integer)

Python's long integers do not refer to the positioning width, i.e. python does not limit the size of long integer values, but the actual number of long integers we use cannot be infinite due to limited machine memory.

Note >> from python2.2, if an integer overflow occurs, Python automatically converts the integer data to grow integers.

    • String

string Definition: quoted is the string
"", "," "" "" ", are all strings
Single and double quotes are no different

string concatenation

name = "jack"age = "22"print(name + age)  # 字符串相加# jack22print(name * 2)  # 字符串相乘# jackjack
    • Boolean type

There are only two types of Boolean types True or False and cannot exist simultaneously

Formatted output
name = "Jason"age = 23job = "it"salary = "10k"info = """"------info of {_name}-----Name={_name}Age={_age}Job={_job}Salary={_salary}---------end-------""".format(_name=name,                              _age=age,                              _job=job,                              _salary=salary)print(info)info2 = """------info of %s-----Name=%sAge=%dJob=%sSalary=%s---------end-------""" % (name, name, age, job, salary)print(info2)info3 = """------info of {0}-----Name={0}Age={1}Job={2}Salary={3} ---------end-------""".format(name, age, job, salary)print(info3)
    • Operator
      Data operations
+  两个数相加-   *% 取模,例如9%4等于1 ** 例如 2**3等于8//  例如9//4等于2比较运算 == 等于!= 不等于<> 不等于(python2,python3没有)><>=<=

Assignment operations

==+=   c+=a等效于c=c+a-=    c-=a等效于c=c-a*=    c*=a等效于c=c*a%=    c%=a等效于c=c%a//=    c//=a等效于c=c//a**=    c**=a等效于c=c**a

Logical operations

and 布尔“与” ,x和y同时为真即x and y为真,否则为假or   布尔“或”,x和y只要有一个为真即 x or y 为真 ,X和y同时为假则x or y为假not  布尔“非”,x为真则返回为假,x为假则返回为真

Member operations

in         如果在指定序列中找到值返回ture,否则返回Fasle.in not   如果在指定的序列中没有找到值就返回ture,否则返回false

Identity operations

is        is是判断两个标识符是不是引用自一个对象is not  is not 是判断两个标识符是不是引用自不同对象

Bit arithmetic

    • & Bitwise AND operation two-bit at the same time as "1", the result is "1", otherwise 0
    • | Bitwise OR operation as long as one is 1 and its value is 1
    • ^ Bitwise XOR or the same as "0", different from "1"

      x=0101,y=1011x^y==1110
    • ~ Reverse the bitwise of a binary number, which will change 0 to 0.
    • << a = a << 2

Move the bits of a to the left 2 bits, and 0 to the right. If the left-hand side does not contain 1, then each left-shift is equal to the number multiplied by 2.

a=0011 0011 #十进制等于51a==a<<2a==1100 1100 #十进制等于204
    • A = a >> 2 shifts the bits of a to the right 2 bits

      a=1100 1100 #十进制为204a==a>>2a=00110011 #十进制为51

      Ternary operations

      a,b,c=1,3,5d=a if a>b else cprint(d)# 5
      Process Control
    • Single Branch

If condition:
Code to execute when the condition is met

a = 1if a < 2:    print(“lalala”)
    • Dual Branch

      a = 1b = 2if a < b:print("right")else:print("error")
    • Multi-Branch

x = 2if x == 1:    print("i am 1")elif x == 2:    print("i am 2")elif x==3:    print("i am 3")else:    print("unknow")
While loop

While condition:
Execute code ...

count = 0while count <= 100:    print("loop", count)    count += 1
Dead Loop

Dead loop

count = 0while True:    print("forever love")    coungt += 1
Break & Continue
    • The break:break is used to completely end a loop, jumping out of the loop after the loop executes the statement
count = 0while count <= 100:    print("loop", count)    if count == 5:        break    count += 1print("---out of while loop---")
    • Continue:continue and break are a bit similar, the difference is that continue just terminates the loop, and then executes the subsequent loop, and break terminates the loop completely.
      ```
      Count = 0
      While Count <= 100:
      Count + = 1
      If Count > 5 and Count < 95:
      Continue
      Print ("Loop", count)

Print ("---out of a while loop---")

### while else当while循环正常执行完,中间没有被break终止的话,就会执行else后面的语句。

Count = 0
While Count <= 5:
Count + = 1
Print ("Loop", Count)
Else
Print ("Loop normal execution")

Print ("---out of a while loop---")
```

Python basic syntax

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.