You can learn python like this! !

I just learned python recently, friends who want to know python can learn it with me~
Insert picture description here

First come the simplest procedure

print("Hello World!")

In fact, python also supports the Chinese version and can be used directly

print("你好")

Variables
The value of a variable can be changed, and it is generally modified by assignment, such as:

a=1
a=a+1
print(a)

The result of the operation is Insert picture description here

Here are a few basic functions:

Insert picture description here

1. Input function input() function

<variable> = input (<indicative text>)
such as the following

a=int(input("请输入第一个数:"))
b=int(input("请输入第二个数:"))
c=a+b
print("两数的和为%d"%(c))

Manually enter a and b to calculate the sum of the two (int stands for integer, and can also be changed to floating point, etc.)

Two. eval() function

It is called "get return value function", which can convert a string to code execution and return the value of the expression.
for example

a=eval("1.2+4.3")
print(a)

The result is 5.5. It should be noted here that you must not forget (" ") in eval(). In fact, this program is also equivalent to

a=1.2+4.3
print(a)

Did you notice the difference?Insert picture description here

In fact, the eval() function is often used together with the input() function to obtain the number entered by the user. The usage is as follows:
<variable> = eval(input(<indicative text>))

value = eval(input("请输入要计算的数值: "))
print(value**2)

(** stands for square)

Three.print() function

This is a function used to output the result of an operation. Depending on the output content, there are the following three uses.
①It is only used to output character strings, and the usage is as follows:
print(<output string>)

print("Hello World!")

This is the procedure we gave at the beginning.
②It is only used to output one or more variables, and the usage is as follows:
print(<variable 1>, <variable 2>,..., <variable n>)
is the same example just now

a=1.2+4.3
print(a)

③It is used to mix output string and variable value, and the usage is as follows:
print(<output string template>.format(<variable 1>, <variable 2>,..., <variable n>))
where format () is required Using braces {} to escape is equivalent to replacing the traditional% with {} to achieve formatted output.
The basic format used is:
<template string>.format(<comma-separated parameters>)
Note that < .precision > starts with a decimal point (.).

a, b = 10,5
print("{}和{}的乘积是{}".format(a, b, a*b))

** Here is another point to explain
to print : Assign a value to the end parameter of the print() function, and use it as follows
print(<content to be output>, end="<end of output added>")

a = 2
print(a, end=".")
print(a, end="%")

The print function will add a newline character ('\n') at the end by default. After
adding the end='' parameter, the newline character will not be added at the end, but an empty string will be added at the end. Whatever end is equal to will be added at the end What?
Of course, you can also write directly like this

a = 2
print(a,".")
print(a,"%")

See here, do you understand a lot?
Insert picture description here
Insert picture description here

After learning these basic functions, let's introduce the basic data types of python.

1. First is the number type

This is very simple, including integer types (for example: 99), floating-point number types (for example: 3.1415), complex number types (for example: 1+3i)

2. Number type operation

1. Numerical operation operators

Python provides 9 basic numerical operation operators
Insert picture description here

2. Numerical calculation functions

The Python interpreter provides 6 built-in functions related to numerical operations
Insert picture description here

Three. String type

A character string is a sequence representation of characters, which is divided into single-line character strings and multi-line character strings according to the content of the character string.

  • A single-line character string can be represented by a pair of single quotation marks (') or double quotation marks (") as boundaries. Single quotation marks and double quotation marks have the same effect.
  • n Multi-line strings can be represented by a pair of triple single quotation marks (''') or triple double quotation marks (""") as the boundary, both have the same effect.
    Introduce several Python language escape characters: \
    For example: \n Represents a new line, \ represents a backslash, 'represents a single quotation mark, "represents a double quotation mark, \t represents a tab character (TAB), etc.
    For strings, Python language provides several basic operators to
    Insert picture description here
    demonstrateInsert picture description here
name = "Python语言" + "程序设计"
print(name)
a = "我是小仙女!" * 3#重要的事情说三遍,哈哈哈
print(a)
name = "Python语言" + "程序设计"
a = "语言" in name
print(a)

The running results are designed in
Python language respectively.
I am a little fairy! I am a little fairy! I am a little fairy!
True
string processing functions
Python language provides some built-in functions for string processing
Insert picture description here

a = len("全国计算机等级考试Python语言科目")
print(a)

The result is that
there are several string processing methods in 19 Insert picture description here
, just understand it → → method is also a function, but the calling method is different.
The function is called in the form of func(x), and the method is called in the form of <a >.func(x). The method only works on the leading object <a >.
Insert picture description here

  • str.split(sep) can separate the string str according to sep, and the divided content is returned as a list type.
  • The str.count(sub) method returns the number of occurrences of sub in the string str, where sub is a string.
  • The str.replace(old, new) method replaces the old string appearing in the string str with the new string. The lengths of old and new can be different.
  • The str.center(width, fillchar) method returns a string of length width, where str is at the center of the new string, and new characters on both sides are filled with fillchar. When width is less than the length of the string, str is returned. Among them, fillchar is a single character.
  • str.strip(chars) removes the characters listed in the left and right chars from the string str. chars is a character string in which every character will be removed.
  • In str.join(iter), iter is an iterative variable. This method inserts the string of str between the elements of the iter variable to form a new string.
    -Let’s write so much for the first blog. Please give me your advice.
    Insert picture description here
    Insert picture description here

Guess you like

Origin blog.csdn.net/m0_49674812/article/details/109439252