Python1-Basic Syntax 1

++++++++++++++ I'll keep up with you, wyb++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1. Interpreter

1. The Role of Interpreters

Run Files: Programmer and Computer Translator

2. Python interpreter types

(1) Interpreter developed in Cpython.C and widely used

(2) IPython: An Interactive Interpreter Based on CPython

2. Variables

1. Define variables

Variable name = value
my_name = 'TOM'
print(my_name)

3. Data Types

# int
num1 = 1
print(type(num1))
# float
num2= 1.11
print(type(num2))
# bool
num3 = False
print(type(num3))
# str
num4 = "Hello"
print(type(num4))
# List list list
num5 = [1,2,3]
print(type(num5))
# Tuple tuple
num6 = (1,2,3)
print(type(num6))
# set collection
num7 = {1,2,3}
print(type(num7))
# dict Dictionary--Key-Value Pairs
num8 = {"name":"Tom","age":12}
print(type(num8))

1. Format output

# Format output string
age = 18
name = "zhaomin"
weight = 90.5
stu_id = 1
# I am Zhangomin, 18 years old, weighing 90.5 kg, school number 001
print("I am%s" % name)
print("Weight:%.2f" % weight)
print("My School Number%03d" % stu_id)
print("I am%s ,This year%d Age, weight is%.2f Jin,School Number%03d" %(name,age,weight,stu_id))

2. Compare%s with f'{}'

# F expression is more efficient than%s and f'{}'format string
print("I am%s ,This year%s Age, weight is%s Jin,School Number%s" %(name,age,weight,stu_id))
print(f"I am{name} ,This year{age}Age, weight is{weight}Jin,School Number{stu_id}")

3. Escape characters\n and\t (backslash)

# Escape Character\n
print("hello\nword")
# Escape Character\t
print("hello\tPython")
print("++++++++++++++++++++++++++++++++++++++++++++++")
# Set the print terminator, default line break\n
print("hello", end="\n")
print("word",end="\t")# tab after self-setup
print("hello",end="...")# Use after setup by yourself...
print("Python")

4. Input function input()

#User input information
"""
Characteristic:
(1) Encountered input and waited for user input
 (2) Receive input storage variables
 (3)input receives a data type of string
"""
password = input("Enter password:")
print(f "Password confirmation: {password}")
print(type(password))

5. Functions for data type conversion

function Explain
int(x,[base]) Convert x to an integer
float(x) Convert x to a floating point number
str(x) Convert object x to string
eval(str) Used to evaluate a valid Python expression in a string and return an object
tuple(s) Convert sequence s to a tuple
list(s) Convert sequence s to a list
# Data Type Conversion Function
num1 = "1"
num = 123
num2 = int(num1) # Convert num1 to integer
print(type(num2))
print(num2)

num3 = float(num1)
print(num3,type(num3))# Convert num1 to floating point

str1 = str(num)# Convert num to string
print(str1,type(str1))

list1 = [1,2,3]
tuple1 = tuple(list1)# Convert a sequence to a tuple
print(tuple1,type(tuple1))
ss = (4,5,6)# Convert a sequence to a list
print(list(ss),type(list(ss)))

str2 = '1'
str3 = '1.1'
str4 = '(1,2,3)'
str5 = '[4,5,6]'
print(eval(str2), type(eval(str2)))
print(eval(str3), type(eval(str3)))
print(eval(str4), type(eval(str4)))
print(eval(str5), type(eval(str5)))

4. Operators

1. Classification of operators

  • Arithmetic operator
  • Assignment Operators
  • compound assignment operators
  • Comparison operator
  • Logical operators

1.1 Arithmetic Operator

operator describe Example
+ plus 1+1=2
- reduce 1-1=0
* ride 2*2=4
/ except 9/2=4.5 9/3=3.0
// To be divisible by 9//4=2
% Modeling 9%4=1
** index 2**4=16
() parentheses (1+2)3=9

Note: Priority

() > ** > * / // % > + -

1.2 Assignment Operator

operator describe Example
= assignment Assign the result = to the right to the variable to the left of the equal sign

1.2.1 Single variable assignment

num1 = 10

1.2.2 Multiple variable assignments

num1,num2,float,str = 1,21,1.1,"qw"

1.2.3 Assigning the same value to more than one variable

a=b=100

Tags: Python IPython

Posted on Tue, 05 May 2020 19:21:01 -0400 by dml