程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python basics 2022 latest] lesson 2 variables & data types

編輯:Python

【Python Basics 2022 newest 】 The second lesson Variable & data type

  • summary
  • Variable
    • Create variables
    • The logic behind it
    • Common mistakes
  • data type
    • Numeric type
    • Non digital
  • Data type conversion
    • Floating point to integer
    • Integer to Boolean
    • Common mistakes

summary

Starting today , Xiaobai, I will lead you to learn Python Introduction to zero foundation . This column will explain + Practice mode , Let's get familiar with it Python The grammar of , application , And the basic logic of the code .

Variable

Variables are values stored in memory , When creating variables, we will open up a space in memory . Variables are used to store data , stay Python Variables in do not need to be declared .

Create variables

Example :

a = 10 # Create variables a, And assignment 10
b = " I'm Xiaobai " # Create variables b, And assignment " I'm Xiaobai "
print(a, b) # Debug output

Output results :

10 I'm Xiaobai

notes : print Function is used to output the value of a variable

The logic behind it

Python The memory space in is divided into three parts : Code section , Static data area , And dynamic data area . Dynamic data area is divided into stack and heap . ( Understanding can )

Simply draw the logic behind the above code :

  • Open up a space in the stack , Create variables a, Variable a Point to the value in the heap 10
  • Open up a space in the stack , Create variables b, Variable b Point to the value in the heap “ I'm Xiaobai ”
  • The output flows through the method in the stack print(), Extract the value corresponding to the variable in the heap , And output it on the console

Common mistakes

error 1, Call variables that are not created :

a = 1 # Definition a Variable
print(b) # Try to output undefined b Variable

Output :

Traceback (most recent call last):
File "C:/Users/Windows/Desktop/ lecture / The first lesson Common errors in variables .py", line 2, in <module>
print(b) # Try to output undefined b Variable
NameError: name 'b' is not defined

data type

stay Python Medium variables do not need to be typed .

Python There are several data types in :

  • Digital :
    • integer (int)
    • floating-point (float)
    • Boolean type (bool)
  • Non digital :
    • character string (string)
    • list (list)
    • Tuples (tuple)
    • Dictionaries (dict)

Numeric type

Example :

a = 1 # plastic (int) Variable
b = 1.23 # floating-point (float) Variable
c = True # Boolean type (bool)
print(a, b, c) # Debug output variable value
print(type(a), type(b), type(c)) # Debug output variable type

Output :

1 1.23 True
<class 'int'> <class 'float'> <class 'bool'>

notes : adopt type() function , python Will return the type of variable .

Non digital

Example :

d = " I'm Xiaobai " # character string (string)
e = [1, 2, 3] # list (list)
f = (1, 2, 3) # Tuples (tuple)
g = {" Course content ": "Python Basics 2022 newest "} # Dictionaries (dict)
print(d, e, f, g) # Debug output variable value
print(type(d), type(e), type(f), type(g)) # Debug output variable type

Output results :

 I'm Xiaobai [1, 2, 3] (1, 2, 3) {' Course content ': 'Python Basics 2022 newest '}
<class 'str'> <class 'list'> <class 'tuple'> <class 'dict'>

notes : adopt type() function , python Will return the type of variable .

Data type conversion

Floating point to integer

Example :

a = 1.23 # Create a floating point (float)
b = int(a) # Convert floating point type to integer type (int)
print(a, b) # Debug output variable value
print(type(a), type(b)) # Debug output variable type

Output results :

1.23 1
<class 'float'> <class 'int'>

Integer to Boolean

Example :

a = 2 # Create integer type (int)
b = bool(a) # Convert integer type to Boolean (bool)
print(a, b) # Debug output variable value
print(type(a), type(b)) # Debug output variable type

Output results :

 True
<class 'int'> <class 'bool'>

notes : Dangfei 0 When the number of is converted to Boolean, it is True, Instead of False.

Common mistakes

Example :

a = " I'm Xiaobai " # Create string
b = int(a) # Strong conversion to integer
print(a, b) # Debug output variable value
print(type(a), type(b)) # Debug output variable type

Output results :

Traceback (most recent call last):
File "C:/Users/Windows/Desktop/ lecture / The first lesson Data type conversion .py", line 21, in <module>
b = int(a)
ValueError: invalid literal for int() with base 10: ' I'm Xiaobai '

notes : Some types cannot be cast .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved