Python based – 1

variable

Variables are values stored in memory. In any programming language, you need to process data, such as numbers, strings, and characters. You can use data directly or save data to variables for later use.

A Variable can be thought of as a little box that “holds” data in a program. Each variable has a unique name, and the data in the variable can be found by the name of the variable

Variable naming

1) identifier
  • Identifiers are defined in the programThe variable nameandThe function name.
  • Identifiers can be specified byThe letter,The underlineanddigitalComposition.
  • The identifier cannot bedigitalAt the beginning.
  • The identifier cannot matchThe keywordThe same name.
  • identifierCase sensitive.
2) key
  • Keywords are identifiers that are already used inside Python.

  • View Python keywords:

import keyword
print(keyword.kwlist)
Copy the code
3) Naming rules of variables

Official naming rules

  • The purpose of naming rules is to increase code identification and readability, and is not absolute or mandatory.

  • When defining variables, it is recommended to leave a space to the left and right of =.

  • When a variable name consists of two or more words, each word should be in lower case and underlined. Such as: first_name.

Hump nomenclature

  • When variable names are made up of two or more words, you can also use camel name.

  • The small hump nomenclature starts with a lowercase letter for the first word and capitalizes the first letter of subsequent words. Such as: firstName.

  • The grand camel nomenclature capitalizes the first letter of every word. Such as: FirstName.

Variable types

Numeric types
  • Plastic – int
  • Float to float
  • Boolean type -bool, Boolean type usedTrueandFalseRepresents true and false
  • The plural -complex, used mainly forScientific computing, such as: plane field problem, wave problem, inductor capacitance and other problems.
The digital type
  • String – the STR
  • List – the list
  • A tuple – tuple
  • The dictionary – dict

Variable assignment

Conventional type of skin

Each variable must be assigned a value before it can be used, and then the variable can be created. The equal sign = is used to assign values to variables. The left side of the equal = operator is a variable name, and the right side of the equal = operator is the value stored in the variable

# -*- coding: UTF-8 -*-
counter = 100 # Assign integer variables
miles = 1000.0 # floating-point
name = "John" # string
 
print(counter)
print(miles)
print(name)
Copy the code
The input value
  • Note: The input variable is a string by default and can be converted if necessary
String variable =input("Warning message:")
Copy the code
Accept function return value assignment
  • If this function has a return value
var = function(param)
Copy the code