Basic knowledge of Python variables

  • 2021-12-09 09:14:29
  • OfStack

Directory 1. What are variables 2. Variable naming rules 3. Keywords and reserved words in python 4. Common variable names 5. Variable assignment 6. Variable data type 6.1. Number 6.2. Boolean type 6.3. String 6.4. type () function

1. What are variables

The so-called variable refers to the amount whose value can be changed during the running of a program.

Example: In mathematics x And y Is the variable, Python The difference in is that variables don't just store numbers, they can store values of any data type.

2. Variable naming rules

Variable names can only include letters, numbers and underscores Numbers cannot be used for 1 character Variable names are case sensitive Keywords and reserved words cannot be used

3. Keywords and reserved words in python

Keyword: Yes Pyt Some words in hon have been given specific meanings, which requires developers not to use these keywords as identifiers to name variables, functions, classes, templates and other objects when developing programs.

Reserved word: The system is retained (it may never be used, but you can't use it).

Get keywords and reserved words:


import keyword
print(keyword.kwlist)

4. Common variable names

Examples:

Type 1: myName   myFriendName Type 2: MyName   MyFriendName Type 2: my_name   my_friend_name

5. Assignment of variables

Python It is a dynamic programming language. In the process of program execution, variables can be given different types of values, and the data types of variables can be changed at the same time. The assignment of variables does not require type declaration, Python Variable types are automatically declared. Assignment operator “=” Used to assign values to variables.

Examples:


a = 0.01
b = 666
c = " Hello, Wrold"
print(a)
print(b)
print(c)
print(a,b,c)


Multivariable assignment

a = 1
b = 1
c = 1
print(a,b,c)
# Code equivalent to 
a = b = c = 1
print(a,b,c)

a = 1
b = 2.2
c = "HelloWorld"
d = True
print(a,b,c,d)
# Code equivalent to 
a , b ,c ,d = 1 , 2.2 ,"HelloWorld" , True
print(a,b,c,d)

6. Data Type of Variable

Python The data types of include numbers, Booleans, strings, tuples, lists, dictionaries, and so on.

6.1. Figures

Numeric data types are used to store numeric values, including the following three types:

a.int (Integer) is commonly referred to as an integer, b.float (Floating-point), also known as floating-point numbers, consists of integers and decimals. c.complex (Complex) Consists of a real number and a complex number; The general form is x + yj.

6.2. Boolean Types

bool (Boolean) is a rather special type that has only True (True) and False (False) Two values (pay attention to case)

6.3. String

str (String) is arbitrary text enclosed in single or double quotation marks, such as ' abc ' , " abc "

Escape characters are defined as Python0 Start, followed by 1 character, for example: Python1

Python It also allows r before single quotation marks to indicate that strings inside single quotation marks are not escaped by default

6.4. type () Function

Function: Returns the type of object


a = 0.01
print(type(a))

Related articles: