Python Basics Day 1 Getting to Know Variables and Data Types

variable

Variable role - used to store and represent data.

By defining and using variables, we can dynamically save and modify data during program execution.

The functions of variables include but are not limited to the following aspects:

  • Storing data : Variables can be used to store various types of data , including numbers, strings, booleans, etc.
  • Data passing : Variables can be passed as parameters to functions or methods to share data between different code blocks.
  • Data operation : Variables can participate in various operations , such as mathematical operations, string concatenation, logical judgment, etc.
  • Data state management : variables can be used to track and manage the state in the program, such as counter variables, flag variables, etc.

variable naming

Proper variable naming is an important factor in writing clear, readable, and maintainable code.

rule
  • Variable names must only consist of alphanumeric underscores
  • Numbers cannot be used as the beginning of the name (support Chinese variable names)
  • System keywords cannot be used as variable names
  • Variable names are case sensitive
nomenclature
big hump

The first letter of each word is capitalized for object-oriented use

FirstName LastName LastNameData

small hump

The first letter of the word is lowercase, and the first letter of the following words is capitalized

firstName lastName lastNameData

Underline

first_name last_name last_name_data

njiax is usually used as a temporary variable
suggestion
  • Use meaningful names: Variable names should reflect the meaning of the data they represent and be easier to read ( see Name Knows Meaning ).
  • Follow naming conventions: usually use lowercase letters and underscores to form variable names, and avoid special characters and spaces .
  • Use CamelCase : For variable names consisting of multiple words, you can use CamelCase ( except for the first word, the first letter of subsequent words is capitalized )
  • Follow naming conventions: In a specific programming language or project, there may be specific naming conventions and conventions that should be followed and used uniformly.

variable type

Common variable types, including global variables, local variables, static variables , instance variables, class variables, parameter variables, etc.
Each variable has its specific scope and life cycle to meet different programming needs.

Global Variables¶

Variables that can be accessed anywhere in the program, they exist throughout the execution of the program. Global variables are usually defined at the top level of the program, and can also be referenced globally through the global keyword inside the function.

Local Variables¶

A variable defined and used within a particular code block, function, or method. The scope of a local variable is limited to the code block in which it is defined, beyond which it cannot be accessed.

Static Variables¶

In object-oriented programming, static variables are variables defined at the class level, they remain constant throughout the life of the class, and always exist in memory from program startup to program shutdown . In contrast to instance variables, static variables belong to the class itself rather than an instance of the class and can be accessed through the class name or instance .

Instance Variables¶

In object-oriented programming, an instance variable is a variable that is assigned to each instance individually during the instantiation of a class and can only be accessed through an instance of the class . The scope of an instance variable is limited to a specific instance object, and different instance objects can have different instance variable values. Instance variables are usually defined in the class's constructor (__init__ method).

Class Variables¶

In object-oriented programming, a class variable is a variable defined at the class level that is shared by all instances of that class . Class variables belong to the class itself rather than the instance and can be accessed either by the class name or the instance . The life cycle of a class variable is associated with the instance of the class, and when the instance is destroyed, the class variable will also be released. ( There are instances of class variables, but they can be accessed through the class name )

Parameter Variables¶

A variable declared within a function or method definition that accepts parameter values ​​passed by the caller . The scope of parameter variables is limited to the execution process of the function or method , and can be passed and accessed through the parameter list.

variable use

Assignment operation= : Assign specific values ​​to variables through assignment operators .
Reference variable : refer to and access the data saved by the variable through the variable name.
Modify variable : You can modify or update the value of the variable as needed.
Scope : Make sure variables are scoped (e.g. global, local) correctly and in line with design intent.
Lifecycle : Understanding the lifecycle of variables is an important factor in ensuring that variables are created, used, and destroyed at the appropriate times.

Reasonable use of variables can improve the readability, flexibility and maintainability of the code, and also help to avoid errors and reduce waste of resources.

type of data

insert image description here

Numeric Types

Integer (int): represents an integer, such as 1, -10, 100.
Floating point number (float): represents a number with a fractional part, such as 3.14, -0.5, 1.0.

String type (String Type)

String (str): Indicates a series of text composed of characters, which can be enclosed in single quotes or double quotes, such as "Hello", 'Python'.

Boolean Type

Boolean value (bool): A value representing True or False, used for logical judgment and control process
such as True and False.

List Type

List (list): Represents a series of values ​​arranged in a specific order, which can contain elements of different types, enclosed in square brackets
such as [1, 2, 3], ['a', 'b', 'c'].

Tuple Type

Tuple: similar to a list, but not modifiable, enclosed in parentheses
such as (1, 2, 3), ('a', 'b', 'c').

Dictionary Type

Dictionary (dict): represents a collection of key-value pairs (key-value), enclosed in curly braces
such as {'name': 'Alice', 'age': 25}.

Set Types

Set (set): Indicates a group of unordered and non-repetitive elements, created with curly braces or set() function,
such as {1, 2, 3}, set([4, 5, 6]).

other types

None type: represents a null or missing value, used to indicate that a variable has no value or a function has no return value.

The above are common data types in Python, and each type has its specific attributes, methods, and uses. You can choose an appropriate data type to store and manipulate data according to actual needs.

Notice

In Python, the integer 0 is not the False value of the Boolean type (bool) , but it can be regarded as a false value (Falsy) .

In a Boolean context, the following values ​​are considered false:
False : A false value of type Boolean.
None : A special object representing a null or missing value.
0 : Zero for integer types.
0.0 : Zero for floating point types.
"" : empty string.
[] : empty list.
() : empty tuple.
{} : empty dictionary.
set() : an empty set.

Summary: 0 and null

These values ​​are considered False in logical expressions , and all other non-false values ​​are considered True. Therefore, 0 can be used to represent a false value under certain conditions .

For example:

my_var = 0
if not my_var:
    print("my_var is a falsy value")

In the above example, the variable my_var is assigned a value of 0 and its true value is checked using the not operator. Since 0 is a falsy value, the condition holds and outputs "my_var is a falsy value".

Note that 0 is still considered a valid integer value in numeric operations or other contexts.

Guess you like

Origin blog.csdn.net/m0_74921567/article/details/132416364