# Introduction to python Programming

This time, our examples and contents may be a little boring, but I will try my best to simplify each knowledge point. I hope you can be patient:

We have seven examples this time, but in general, they can be divided into two parts. First, let's take a look at the first part:

# Number type and its operation:

```#DayDayUpQ1.py this small program is used to calculate how much we can make progress or retreat in a year if we make progress or retreat by 1% every day
dayup=pow(1.001,365)  #Define a dayup variable to calculate the 365 power of 1.001
daydown=pow(0.999,365)
print("Upward:{:.2f},down:{:.2f}".format(dayup,daydown))
```
```#DayDayUpQ2.py
dayfactor=0.005
dayup=pow(1+dayfactor,365)
daydown=pow(1-dayfactor,365)
print("Upward:{:.2f},down:{:.2f}".format(dayup,daydown))

#You can think about why we should quote a dayfactor here```
```#DayDayUpQ3.py
dayup=1.0
dayfactor=0.01
for i in range(365):
if i%7 in [6,0]:
dayup=dayup*(1-dayfactor)
else:
dayup=dayup*(1+dayfactor)
print("Power of the workday:{:.2f}".format(dayup))

#This is our progress on weekdays, retreat on weekends and progress in a year```
```#DayDayUpQ4.py
def dayUP(df):
dayup=1
for i in range(365):
if i%7 in [6,0]:
dayup=dayup*(1-0.01)
else:
dayup=dayup*(1+df)

return dayup
dayfactor=0.01
while dayUP(dayfactor)<37.78:
dayfactor+=0.001
print("The working day effort parameters are:{:.3f}".format(dayfactor))```

After learning the knowledge of the previous two articles, I believe that my friends will be able to understand these four codes. Let's take a look at the knowledge points:

## 1, Integer type:

I believe you must be familiar with the integer type. Here we introduce a function pow(x,y). This function is used to calculate the Y power of X. you can calculate as much as you want

```dayup=pow(1.001,365)  #Define a dayup variable to calculate the 365 power of 1.001
daydown=pow(0.999,365)```

Then let's take a look at the 4-ary representation of integers:

Decimal: 1010, 99

Binary: starts with 0b/0B: 0b010

Octal: starts with 0o/0O: 0o123

Hex: starts with 0x/0X: 0x89

## 2, Float type:

Floating point numbers are numbers with decimals, and their value range and decimal precision are limited. It is worth noting that there is uncertainty in the operation between floating-point numbers, but this is not a bug. It is determined by the code. Partners can calculate 0.1 + 0.3 and 0.1 + 0.2. Here is a function:

The function of round(x,d) is to round x, and D is to intercept the decimal places. We generally use the round function as an auxiliary for floating-point operation and comparison

Scientific counting method: < a > e < b > represents a * (the B power of 10)

## 3, Plural type:

Yes, it's a complex number, and it's a complex number consistent with the concept in mathematics,

z=a+bj

z.real obtains the real part of the complex number, and z.imag obtains the imaginary part of the complex number

## 4, Numeric operators:

Here is +, -, *, /;

//This represents the division of an integer. For example, the result of 10 / 3 is 3

+x. + y, x%y, x**y are the same as in C language

February operator:

What are binary operators? Operators such as + =, - =, * =, / =

## 5, Numerical operation function:

abs(x): This is used to find the absolute value of X

divmod(x,y): quotient remainder, (x//y,x%y), and output quotient and remainder at the same time

pow(x,y[,z]): power remainder, (x**y)%z, [..] Indicates that parameters can be omitted

round(x[,d]): as mentioned earlier, it is used for rounding

max(x1,x2...): Returns the maximum value

min(x1,x2,x3): returns the minimum value

int(x): converts x to an integer

float(x): converts x to a floating point number

complex(x): convert x into a complex number and add the imaginary part

OK, let's understand these in the first part. Let's take a look at the second part:

# String type and operation:

```#TextProBarV1.py here, let's implement a simple progress bar
import time
scale=10
print("------Execution start------")
for i in range(scale+1):
a='*'*i
b='.'*(scale-i)
c=(i/scale)*100
print("{:^3.0f}%[{}->{}]".format(c,a,b))
time.sleep(0.1)
print("------end of execution------")```
```#TextProBarV2.py here we implement a single dynamic refresh
import time
for i in range(101):
print("\r{:3}%".format(i),end="")
time.sleep(0.2)```
```#TextProBarV3.py
import time
scale = 50
print("Execution start".center(scale//2, "-"))
start=time.perf_counter()
for i in range(scale+1):
a = '*' * i
b = '.' * (scale - i)
c = (i/scale)*100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')
time.sleep(0.1)
print("\n"+"end of execution".center(scale//2,'-'))```

### 1, Representation of string type

I have introduced the operations of string indexing, slicing and representation in the first article, so I won't go into too much detail here

Let's take a look at some special characters that appear this time:

\b: Indicates fallback

\n: Line feed, the cursor moves to the beginning of the next line

\r: Press enter to move the cursor to the beginning of the line

### 2, String operator

This time:

x+y: This is not an operator. This is to connect two strings together. For example, the result of string "str"+"lop" is strlop

n*x: This indicates that the string x is copied n times

x in s: returns True if x is a substring of s; otherwise, returns False

Let's take another look at the operator to get the week:

```#weeknameprint.py
weekstr="Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"
weekid=eval(input("Please enter the number of weeks (1)-7):"))
pos=(weekid-1)*3
print(weekstr[pos:pos+3])```

```#weeknameprint.py
weekstr="123456"
weekid=eval(input("Please enter the number of weeks (1)-7): "))
print("week"+weekstr[weekid-1])```

### 3, String handler

Length of returned string (len): X

str(x): this function is just the opposite of eval function, which is used to convert a number into string form

hex(x): hexadecimal or octal lowercase string of integer X

chr(u): u is Unicode encoding and returns the character corresponding to the encoding

ord(x):x is a character and returns its corresponding Unicode encoding

Interesting stuff:

We can try the output:

```for i in range(12):

print(chr(9800+i),end="")```

This string of characters can output twelve constellations. Come and find your constellations

### 4, String processing method

Method is a proper noun in programming, and it is also a function itself

str.lower()/str.upper(): returns a copy of a string, all characters being lowercase or uppercase

str.split(sep=None): returns a list consisting of the parts of str separated according to sep

str.count(sub): returns the number of times the string appears in str

str.replace(old,new): returns a str copy of the string. All old strings are replaced with new

str.center(width[,fillchar]): the string is centered according to the width. Fillchar is optional

str.strip(chars): removes the characters listed in chars on the left and right of str

str.join(iter) ": add a str after each element except the last element of iter variable"“

### 5, Formatting of string types

Here we can study < > format(<>)

# Introduction to time library:

## Program timing:

Program timing refers to the process of measuring the time experienced by the start and end actions

Measurement time: perf_counter()

Generation time: sleep()

Keywords: Python Back-end

Added by salmon on Thu, 03 Mar 2022 18:15:38 +0200