INT104
Python Basic Grammar
-
Creart a comment
#this is a comment.
-
Multiline comments
""" This is a comment written in more than one line """
- print() function
print("Hello World") print("Hello", "how are you?") x = ("apple", "banana", "cherry") print(x)
-
Variables
x=4 #x is of type int x="Sally" #x is now of type str #If you want to specify the data type of a variable #this can be done with casting x = str(3) # x will be '3' y = int(3) # y will be 3 z = float(3) # z will be 3.0 x, y, z = "Orange", "Banana", "Cherry" #If you have a collection of values in a list, tuple etc. #Python allows you to extract the values into variables. #This is called unpacking fruits = ["apple", "banana", "cherry"] x, y, z = fruits
-
Data types
If you want to specify the data type, you can use the following constructorfunctions:
-
Operators
-
Arithmetic operators(算数运算符)
# 基本和Java一样 #** Exponentiation 求幂 x**y #// Floor division 整除 x//y #5//2=2
-
Assignment operators(赋值运算符)
- Assignment operators are used to assign values to variables:
- Assignment operators are used to assign values to variables:
-
Comparison operators(比较运算符)
- 和Java一样
-
Logical operators(逻辑运算符)
#and x<5 and x>10 #or x<5 or x>10 #not not(x<5 and x<10)
-
Identity operat
-