Python basics-variables and simple data types

In Python, storing a data requires something called a variable

num1 = 100 #num1 is a variable, like a vegetable basket, the variable is used to store data

num2 = 200 #num2 is also a variable

nums = num1+num2
#accumulate the data in num1 and num2 and put them in the nums variable print(nums) #print in python is to output the
output result: 300

Explanation: The
so-called variable can be understood as a vegetable basket. If you need to store multiple data,
the simplest way is to use multiple variables. Of course, you can also use one

		**程序就是用来处理数据的,而变量就是用来存储数据的**

In the program:

In order to make full use of memory space and manage memory more efficiently, there are different types of variables, as shown below:
Data type:
Insert picture description here

How to know the type of a variable?

In python, as long as a variable is defined and it has data, then its type is already determined. We don't need our developers to actively explain its type. The system will automatically recognize
that type (the name of the variable) can be used. To see the type of the variable

Identifiers and keywords

· Some symbols and names customized by the developer in the program.
·Identifiers are defined by themselves, such as variable names, function names, etc.

Rules for identifiers

·Identifiers consist of letters, underscores and numbers, and numbers cannot begin with
·Identifiers in python are case sensitive
Jun /= jun

<3> Naming rules

·见名知意
	起一个有意义的名字,尽量做到看一眼就知道是什么意思(提高代码可读性)比如:名字就定义未name,定义学生用 student

<4> Naming method
Insert picture description here
Lower camel case: The first word starts with a lowercase letter; the first letter of the second word is capitalized, for example: myName, aDog

Upper camel case: The first letter of each word is capitalized, for example: FirstName, LastName

There is also a nomenclature that uses an underscore "_" to connect all words, such as send_buf,

Python's command rules follow the PEP8 standard, which will be slowly discussed later.

<5>Keyword

What are keywords

Some python identifiers with special functions, these are the so-called keyword keywords, which are already used by python, so developers are not allowed to define their own identifiers with the same name as the keyword

View keywords:

and as assert break class continue def del
elif else except exec finally for from global
if in import is lambda not or pass
print raise return try while with yield

You can view the keywords of python in the current system through the following commands in the Python Shell

import keyword
keyword.kwlist

Input and output

Output

Output of variables in python

Printing tips

print('hello world')
print('Savadika—Thai, hello world ')

Output of variables in python

Printing tips

print('hello world')
print('Savadika-Thai, hello meaning') Copy
2. Formatted output
<1> The purpose of formatting operation For
example, the following code:

pirnt("I am 10 years old")
pirnt("I am 11 years old")
pirnt("I am 12 years old")
…Copy
think about it:

When outputting age, I used "I am xx years old" many times. Can you simplify the procedure? ? ?

answer:

String formatting

<2>What is formatting, see the following code:

age = 10
print("I am %d years old this year"% age)

age += 1
print("I am %d years old this year"% age)

age += 1
print("I am %d years old this year"% age)

…Copy
In the program, I saw an operator like %, which is the formatted output in Python.

age = 18
name = "xiaohua"
print("My name is %s, my age is %d"% (name, age))Copy

<3>Common format symbols

The following is a complete list of which can be used with the% symbol:

Format symbol conversion
%c character
%s character string
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letter 0x)
%X hexadecimal integer (uppercase letter 0X)
%f Floating point number
%e Scientific notation (lowercase'e')
%E Scientific notation (uppercase "E")
%g Shorthand for %f and %e
%G Shorthand for %f and %E

  1. Newline output
    When outputting, if there is \n, then the content after \n will be displayed on another line

print(“1234567890-------”) # will be displayed on one line

print("1234567890\n-------") # One line shows 1234567890, and the other line shows-------Copy

2.4 Operators
Python supports the following operators

  1. Arithmetic Operators.
    Let's take a=10 and b=20 as an example for calculation

Operator description example

  • Add two objects and add a + b to output the result 30
  • Subtract to get a negative number or subtract one number from another a-b output result -10
  • Multiply two numbers or return a string that is repeated several times a * b Output result 200
    / Divide b / a Output result 2
    // Round and divide return the integer part of the quotient 9//2 Output result 4, 9.0/ /2.0 Output result 4.0
    % Take the remainder and return the remainder of the division b% a Output result 0
    ** Exponent a**b is 10 to the 20th power, output result 100000000000000000000
    Note: In mixed operation, the order of priority is: ** higher * /% // higher than + -, in order to avoid ambiguity, it is recommended to use () to handle operator precedence.

In addition, when different types of numbers are mixed, the integers will be converted into floating point numbers for calculations.

10 + 5.5 * 2
21.0

10 + (5.5 * 2)
21.0Copy

  1. Assignment operator Operator
    description example
    = Assignment operator assigns the result on the right side of the = to the variable on the left, such as num = 1 + 2 * 3, and the value of num is 7

Single variable assignment

num = 10
num
10

Multiple variable assignment

num1, num2, f1, str1 = 100, 200, 3.14, “hello”
num1
100

num2
200

f1
3.14

str1
"hello"Copy

  1. Compound assignment operator Operator
    description example
    += addition assignment operator c += a is equivalent to c = c + a
    -= subtraction assignment operator c -= a is equivalent to c = c-a
    *= multiplication assignment operator c *= a is equivalent to c = c * a
    /= division assignment operator c /= a is equivalent to c = c / a
    %= modulo assignment operator c %= a is equivalent to c = c% a
    * *= Power assignment operator c **= a is equivalent to c = c ** a
    //= Divide assignment operator c //= a is equivalent to c = c // a

2.5 Data type conversion

1. Commonly used data type conversion
Function description
int(x [,base ]) Convert x to an integer
float(x) Convert x to a floating point number
complex(real [,imag ]) Create a complex number, real is the real part , Imag is the imaginary part
str(x) converts the object x into a string
repr(x) converts the object x into an expression string
eval(str) is used to calculate the effective Python expression in the string and returns an object
tuple(s) converts the sequence s into a tuple
list(s) converts the sequence s into a list
chr(x) converts an integer to a Unicode character
ord(x) converts a character to its ASCII integer value
hex (x) Convert an integer to a hexadecimal string
oct(x) Convert an integer to an octal string
bin(x) Convert an integer to a binary string
2. Example

int(): Convert data to int type

str1 = “10”

int() is displayed after conversion in decimal by default

num1 = int(str1)

int() handles floating-point numbers, leaving only the integer part and discarding the decimal part (not a rounding operation)

num2 = int(3.74)
print(num2)
3

"""
… num1 = int(str1, 8) # The second parameter is 8, which means it will be displayed after conversion in octal, and the result is 8
… num1 = int(str1, 16) # # The second parameter is 16, It means that it will be displayed after conversion in hexadecimal, and the result is 16
… #01 02 03 04 05 06 07 10
… #01 02… 0B 0C 0D 0E 0F 10
… print(num1)
… “”"

float() converts data into floating point numbers

str2 = “3.14”
f1 = float(str2)
print(type(f1))
<class ‘float’>

f2 = float(10)
print(f2)
10.0

complex() creates a complex number: the first parameter is the real part of the complex number, the second parameter is the imaginary part of the complex number

c1 = 10 + 4j
c2 = complex(10, 4)

print(c1)
(10+4j)

print(c2) # Same as c1
(10+4j)

str(): Convert to string type

num1 = 10
f1 = 3.14

print(type(str(num1)))
<class ‘str’>

print(type(str(f1)))
<class ‘str’>

repr(): converted to expression string

num1 = 10
print(type(repr(num1)))
<class ‘str’>

eval(): Convert data in string form to the original type

str1 = “3.14”
print(type(eval(str1)))
<class ‘float’>

str2 = “[10, 20, 30]”
l = eval(str2)
print(type(l))
<class ‘list’>

chr: Convert an integer to the corresponding Unicode character

s = chr(1065)
print(s)
Щ

ord: Convert a character to the corresponding character code number

n = ord(“A”)
print(n)
65

bin: convert an integer to binary

print(bin(1024)) # 0b starts with the binary number
0b10000000000

oct: convert an integer to octal

print(oct(1024)) # 0o starts with octal number
0o2000

hex: convert an integer to hexadecimal

print(hex(1024)) # 0x starts with hexadecimal
0x400

Guess you like

Origin blog.csdn.net/CHINA_2000chn/article/details/108432846