Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

Variables and types

A variable is a carrier for storing data, that is, a container. A variable in a computer is actual data, or a piece of memory in memory where data is stored. The value of a variable can be read and modified. This is the basis of all computers and controls. Computers contain a wide variety of data types, such as text, video, audio, and so on.

Common Python types

  • Integer: Python can handle integers of any size and supports binary, octal, and hexadecimal representations.
  • Floating point: Floating point numbers are also known as decimals.
  • String: A string is any text enclosed in single or double quotation marks' 'Or double quotation marks""Wrapped string, and can be written as multiple lines (starting with three single or double quotes, ending with three single or double quotes).
  • Boolean: Boolean value onlyTrue,FalseTwo values, eitherTrue, eitherFalse.

The variable command

Define a name for each variable, the Python variable command rules

  • Variable names consist of letters (generalized Unicode characters, excluding special characters), numbers, and underscores, and cannot start with digits.
  • Case sensitive (uppercaseaAnd a lowercaseAAre two different variables).
  • Do not conflict with keywords (words with special meaning, covered below) and system reserved words (names of functions, modules, and so on).

Use of variables

If you define a variable and assign a value to it, you can call it directly from the function as follows:

Print (a, b, c, d) print(a, b, c, dCopy the code

Detect the type and conversion of a variable

Test variable type

The change type is detected by the type() function as follows:

Print (type(a), type(b), type(c), type(c), type(d)) # <class 'int'> <class 'float'> <class 'str'> <class 'bool'>Copy the code

Type conversion

You can use Python’s built-in functions to convert variable types, as follows:

  • int(): Converts a value or string to an integer. Base can be specified.
  • float(): Converts a string to a floating point number.
  • str(): Converts the specified object to a string. Encoding can be specified.
  • chr(): Converts an integer to a string (one character) corresponding to the encoding.
  • ord(): Converts a string (a character) to its encoding (an integer).

The sample code looks like this:

num = 10

str = str(num)

bool = bool(str)

print(type(str))  # <class 'str'>

print(type(bool))  # <class 'bool'>
Copy the code

The operator

The operator describe
[][:] Subscript, slice
** index
~+,- I’m going to put it backwards, plus and minus
*/,%,// Multiplication, division, modulus, divisible
+,- Add and subtract
>><< To the right, to the left
& Bitwise and
^, ` `
< =,<>,> = Less than or equal to, less than, greater than, greater than or equal to
= =! = Equal to, not equal to
is,is not Identity operator
in,not in Member operator
not,orand Logical operator
=,+ =,- =,* =/ =% =/ / =,* * =,& =, ` =^ =,> > =,< < = `
  • The operators in the table above are sorted roughly from highest to lowest
  • The identity operator is understood asisornot
  • The member operator is understood asinorNot in
  • Logical operators join Boolean types,andIf both are true, the result is true, and if one of them is false, it’s false;orIf one of them is true, it is true. If the left is true, the right will not be executed (short-circuit principle).notI take the inverse.
  • The assignment operator assigns the value on the right to the variable on the left
  • Of the compound assignment operatora+=bisa=a+bAnd the others are similar

Input function and use of placeholders

Use the input() function to get keyboard input(string).

Placeholders, as the name suggests, are symbols inserted into the output. Among them

  • %dIs a placeholder for an integer
  • %fIs a placeholder for decimals
  • %sIs a string placeholder
  • % %Represents a percent sign (because a percent sign represents a placeholder, you must write a percent sign in a placeholder string% %)
Aa = input(" input ") bb = int(input(" input ") cc = float(input(" input ") print(" %s" % aa) print(" this is the input integer is: %d" % bb) print(" this is the input float is: %f" % cc)Copy the code

conclusion

  1. Learn what variables do, how they are named, and how they are used
  2. You know the functioninput(),type(), and the use of various functions that convert types
  3. The assignment operator has the lowest precedence. You can pass the assignment operator if you have a good understanding of precedence(a)To increase its priority.