• Variable definitions

    • The = sign is used to assign values to variables
    • = Left is the name of the variable
    • = On the right are values stored in variables

In Python, each variable must be assigned a value before it is used, and only the assigned variable is created.

a = 10
a = b = c = 20
a, b, c = 10.20.30
Copy the code

The data type

  • Number (Number)

    • The integer int

    • Float float

    • The Boolean bool

      • True True
      • False False
    • The plural type complex

  • String(String)

  • The List (List)

  • The Tuple (a Tuple)

  • Set (Set)

  • The Dictionary (Dictionary)

Variables in Python do not have types; only the values held in variables have data types.

You can use the type() function to see the data type of the content held in the variable.

Python provides six basic types :Number, String, List, Tuple, Set, and Dictionary. Among them, List, Tuple, Set and Dictionary are compound data types.

The code example

  • Digital type

    • A = 100 # int

    • B = 3.14 # float type

    • C = True # bool

    • D = 1 + 2i # complex type

  • string

Strings in Python are enclosed in single or double quotation marks, requiring a uniform format throughout the program.

cat = "Tom"
mouse = 'Jerry'
Copy the code
  • The list of

A list is an ordered collection of data types whose elements can be of any data type. The list data is written in the middle of [] to separate each element. Element values in a list can be evaluated using element subscripts.

myList = [1."hello Python"[1.2.3]]
print(myList[1])
Copy the code
  • tuples

A tuple is an ordered set of data types whose elements can be of any data type. The tuple data is written in the middle of () to split each element. Element values in tuples can be evaluated using element subscripts.

myTuple = (1."Hello Python"[1.2], (1.2))
print(myTuple[2])
Copy the code
  • A collection of

A collection is an unordered collection data type with no duplicate elements. It can only store elements of the Number, String, and Tuple types. Set data is written in the middle of {} to separate each element. If there are duplicate elements in the definition collection, it removes them.

mySet = {1.2."set", (1.2)}
mySet = {1.1.2."set", (1.2)}  # Get rid of a 1
Copy the code
  • The dictionary

A dictionary is a mapped set data type whose element is an unordered combination of keys and values. The elements inside are delimited by {} between the elements used, split, keys and values used: split. Dictionary values use Key to obtain Value.

student = {"name": "Lily"."age": 18."sex": "Female"}
print(student["name"])  # fetch Value with key = name
Copy the code

Code Exercise 1

  • Python annotation

Functions: 1. Interprets code 2. Facilitates debugging

Comments are ignored automatically when compiling or interpreting code files. Comments are mainly for your own use

Single-line comment shortcut key: Ctrl+/

  • Now I want to output Chinese

Print (“中文”) print() is a function

Print (“hello”) top line

Identifier The name given to a variable, function, or class

  • The rules

    • The value contains characters, digits, and underscores (_)

    • You can’t start with a number

    • The suggestion is known by name

    For example, if there are more than two words, capitalize the first letter of the second word

  • Variables are used to store data and facilitate manipulation of variable names = values

    • Variable names comply with the rules for identifiers

    • You don’t need to define a type to declare a variable in Python (weakly datatyped languages)

    • Values are typed, and the type of a variable is determined by the type of the value

    • The data stored in a variable can be changed

age=20
print(age)  
 
age2=18
age_2=22

userName='Joe'

price=9.9
price=19.9
print(price) 

a=10
print(id(a))  #id(a) returns the address of the variable
b=a 
a=20
print(a)  # 20
print(b)  # 10

print(id(a))    #id(a) returns the address of the variable
print(id(b))   
Copy the code

Code Exercise 2

  • Python data types

    • number

      • Integer – int
      • The decimal, float
      • Boolean – bool
    • Tr string

    • The tuple tuples

    • List the list

    • The set set

    • Dict dictionary

age=10
print(type(age))   # check the data type int-integer
age=15.5
print(type(age))   # float - float number (decimal number)
age=True
print(type(age))   # bool Boolean values: True(1), False(0)Define strings using quotation marks (single or double) name='Joe'    
name="abc"A string defined in triple quotes can cross the line name=Lisi ABC LisiContinuation character \ name='abc\
lisi'
print(name)
print(type(name))   # STR string,string
Copy the code