Skip to main content

变量和声明 Variable & Statement

声明 Expression and Statement

Expression

1+1
"Beau"

Statement

name = "Beau"
print(name)

You can add semicolon to have more than one statement on a single line.

name = "Beau"; print(name)

注释 Comments

# this is a commented line

Data types

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool
  • Binary Types: bytes, bytearray, memoryview
  • None Type: NoneType
name = "Beau"
print(type(name))
print(type(name) == str) # true
# The isinstance() function checks if the object (first argument) is an instance or subclass of classinfo class (second argument).
print(isinstance(name, str)) # true
age = 2
age_2 = 2.9
print(isinstance(age, int)) # true
print(isinstance(age, float)) # false
print(isinstance(float(age), float)) # true
print(isinstance(age_2, float)) # true

变量 Variable

player_choice = "good"

valid

name_1
HEIGHT
my_name
_name

Not valid

Not start with number and others:

123
1_name
test!
test%

Not key word:

for, if, while, import

字符 String

player_choice = "good"
player_choice2 = 'good'
concat_string = "a" + player_choice
print("concat_string", concat_string)
concat_string += player_choice2 # concat_string = concat_string + player_choice2


print(""" Beau is

39

years old

""")

f-string

age = 25
print(f"Jim is {age} year old.")

print(f"concat_string {contact_String}")

Method

"beau".upper()
"beau".lower()
"bEAU person".title() # Beau Person

"beau".islower()
"au" in "beau" # True
len("beau") # 4

Python String Methods: https://www.w3schools.com/python/python_ref_string.asp

Escaping characters

"Beau"
"Be"au" # this line is error
"Be\"au" # Good
'Be"au' # Good
'Be"\'au' # Good
"Be\nau"
"Be\\au"

String Characeters & Slicing

name = "Beau"
print(name[1]) # e
print(name[0]) # B
print(name[-1]) # u

print(name[1:2]) # e
print(name[1:3]) # ea

布尔值 Boolean

done = True
done = False
done = False # no
done = 0 # no
done = 10 # yes
done = -1 # yes
done = "" # no
done = "abc" # yes

if done:
print("yes")
else:
print("no")

any: it returns true if any item of the values of the iterable is true.

book_1_read = True
booke_2_read = False

read_any_book = any([book_1_read, booke_2_read]) # True
print(read_any_book)
ingradients_purchase = True
meal_cooked = False
ready_to_serve = all([ingradients_purchase, meal_cooked]) # False

数字 Number

num1 =  2+3j
num2 = complex(2, 3)

print(num.real, num.imag)

Built-in Founctions

abs(-5.5) # 5.5
abs(5.5) # 5.5

round(5.5) # 6
round(5.49) # 5
round(5.49, 1) # 5.5

枚举 Enums

from enum import Enum

class State(Enum):
INACTIVE = 0
ACTIVE = 1

print(State.ACTIVE) # State.ACTIVE
print(State.ACTIVE.value) # 1
print(State(1)) # State.ACTIVE
print(State["ACTIVE"]) # State.ACTIVE

print(list(State)) # [<State.INACTIVE: 0>, <State.INACTIVE: 1>]
print(len(State)) # 2

操作符 Operator

Arithmetric Operators

1 + 1 # 2
2 - 1 # 1
2 * 2 # 4
4 / 2 # 2
4 % 3 # 1
4 ** 2 # 16
3 // 2 # 1
"a" + "b" # ab
age += 8 # age = age + 8

Comparison Operators

a = 1
b = 2

a == b # False
a != b # True
a > b # False
a <= b # True
condition1 = True
condition2 = False

not condition1 # False
condition1 and condition2 # False
condition1 or condition2 # True
print(0 or 1) # 1
print(False or "hey") # 'hey'
print('hi' or 'hey') # 'hi'
print([] or False) # False
print(False or []) # '[]'

print(0 and 1) # 0
print(1 and 0) # 0
print(False and "hey") # False
print('hi' and 'hey') # 'hey'
print([] and False) # []
print(False and []) # False

Bitwise opertors

& performs binary AND
| performs binaty OR
^ performs a binary XOR operation
~ performs a binary NOT operation
<< shift left operation
>> shift right operation

is & in operators

  • is: identity operation
  • in: membership operator, in list or other sequance

三元运算 Tenary operator

def is_adult(age):
if age >18:
return True
else:
return False

def is_adult2(age):
return True if age >18 else False

用户输入 User input

age = input("What is your age?")
print(f"your age is {age}")