Python syntax summary

If you've studied any one programming language, because the basic grammar of each language to do basic fact of the matter is the same, only that way or some place a little different, so when will it learn Python other you have mastered the contrast with the programming language to learn, so learn it faster, the effect is better. The following will compare Python basic grammar and basic grammar of JavaScript:

 

JavaScript

Python

Logic

Logical NOT

!

not

Logically true

true

True

Logical false

false

False

Conditional

 

if (condition) {

    ...

} Else if (condition) {

    ...

}else{

    ...

}

if conditions:

    ...

elif condition:

    ...

else:

    ...

The for loop (something only the collection of circulation)

 

for x in the collection:

...

for(var i =0; i<x;i++){

...

}

while (any object can be cyclic)

 

i = 0
numbers = []

while i < 6:
    print(f"At the top i is {i}")
    numbers.append(i)
    i += 1
    print("Numbers now:", numbers)
    print(f"At the botton i is {i}")

 

Operators

 

@: Take divisible - down to the nearest integer divisor close

 

 

//=:

 

++: increment

no

Have

--:cut back

no

Have

+=

Have

Have

-=

Have

Have

String escaping

\\

Backslash

with

\’

apostrophe

with

\”

Double quotes

with

\a

Bell

with

\b

Backspace

with

\f

Form filling

with

\n

Newline

with

\r

Enter

with

\t

Tabs

with

\ v

Vertical tab

with

 

Other Python syntax:


String:

String output variables:

x = "hello"
print(f"I said: {x}") #3.6以上版本支持

print("It's fleece was white as {}.".format("snow")) name= input("Please input your name: ") print("Hello, %s good morning!" %name) print("Hello, %s good morning!" %'Samve')

Do the current line with newline characters:

print("abc", end=" ")

print("efg")

''' '''/""" """作用:

1, multi-line comment;

2, exactly between the output content;

"\"作用:

1、转义特定字符,如:", ', \;

2、表示无法录入的字符,如:\n,\t,\r;

3、连接不同行的文本;

函数:

def secret_formula(started):
    jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars/ 100 return jelly_beans, jars, crates start_point = 10000 beans, jars, crates = secret_formula(start_point) print("With a starting point of:{}".format(start_point)) print(f"We'd have {beans} beans, {jars} jars, and {crates} crates")

*args里的*是什么意思?

它的功能是告诉Python把函数的所有参数都接收进来,然后放到名叫args的列表中去。和一直在在用的argv差不多,只不过前者是用在函数上。

def print_two(*args):
    arg1, arg2 = args
    print(f"arg1: {arg1}, arg2: {arg2}")
    
print_two("Zed", "Shaw")    

 

Guess you like

Origin www.cnblogs.com/samve/p/11145078.html