# Weekend homework - circular exercises

### ***

1. Output 'hello world', input input, output print

```print('hello world')

```
2. Define variables, variable names and identifiers, which are composed of letters, numbers or underscores, and numbers cannot start. Variables cannot be named with keywords. Keywords - some identifiers with special functions or special meanings in python.

a = 15

print(a)

3. Operator + - * /% / / + = - + / =*=

a += 1 #a = a + 1

4. Branch and loop

Branch: single branch, multi branch

#### 1).if branch

if condition:

Execute statement single branch

if condition:

Execute statement

else:

Execute statement single branch

if... # multi branch

if...

if...

if... # multi branch difference: conditions are mutually exclusive

elif...

elif...

#### 2) for loop

```for variable in sequence:   #Let variables take values from the sequence, one by one, until they are finished; Take a value and execute the loop body once.
for The number of cycles is related to the number of elements in the sequence.

for x in 'abc':    #Cycle three times
```

#### 3)while

while True: # infinite loop, only False will jump

continue and break - can only be used in the body of a loop

continue function: end a cycle (directly enter the next cycle)

1. Judge how many primes there are between 101-200 and output all primes.

```count = 0
for num in range(101,200):
for x in range(2, num):
if num % x == 0:
break
else:
count += 1
print(count,num)

```
2. Find the cumulative value of integers 1 ~ 100, but it is required to skip all numbers with 3 bits.

```count = 0
for a in range(1,100):
if a % 10 != 3:
count += a
print(count)
```
3. There is a sequence of fractions: 2 / 1, 3 / 2, 5 / 3, 8 / 5, 13 / 8, 21 / 13... Find the 20th fraction of this sequence

```a = 1
x = 2
count = 0
while True:
x, a = (x + a), x
count += 1
if count == 19:
print("Twentieth score", x, "/", a)
break
```
4. Write a program to calculate the factorial n of n! Results

```n = int(input('Please enter an integer'))
a = 1
for x in range(1,n+1):
a *= x
print(a)
```
5. Ask for 1 + 2+ 3!+…+ 20! And

```n = int(input('Please enter an integer'))
a = 1
count = 0
for x in range(1,n+1):
a *= x
count += a
print(a,count)
```
6. Write a program to find the result of the expression a + aa + aaa + aaaa +... Where a is a number from 1 to 9, and the number of summation items is controlled by n. (A and N can be expressed as variables)

For example: when a is 3 and n is 5: 3 + 33 + 333 + 3333 + 33333

```a = int(input('Please enter an integer'))
n = int(input('Please enter an integer'))
count = 0
for x in range(0,n):
c = 10**x
b = a*c
count += b
if x == n-1:
print(count)
else:
print(count,'',sep='+',end='')
```
1. Console output triangle

```a.according to n The corresponding shape is output according to the different values of
n = 5 Time             n = 4
*****               ****
****                ***
***                 **
**                  *
*

b.according to n The corresponding shape is output according to the different values of(n Odd number)
n = 5               n = 7
*                    *
***                  ***
*****                *****
*******

c. according to n The corresponding shape is output according to the different values of
n = 4
1
121
12321
1234321

n = 5
1
121
12321
1234321
123454321
```
```n = int(input("input n Value of:"))
while True:
for x in range(n):
print("*", end="")
n -= 1
print()
if n == 0:
break

```
```n = int(input('Please enter an odd number:'))
for x in range(1, n + 1, 2):
y = int((n - x) / 2)
print(y*' ', x * '*')
```
2. Xiaoming's unit issued a 100 yuan shopping card. Xiaoming went to the supermarket to buy three kinds of washing and chemical products, shampoo (15 yuan), soap (2 yuan) and toothbrush (5 yuan). If you want to spend 100 yuan exactly, what combination of purchase can you have?

```for a in range(0,7):
for b in range(0,50):
for c in range(0,20):
if 15*a+2*b+5*c == 100:
print(a,b,c)
```
3. The thickness of a piece of paper is about 0.08mm. How many times can it be folded in half to reach the height of Mount Everest (8848.13m)?

```count = 0
c = 0.08
while True:
c *= 2
if c <= 8848130:
count += 1
else:
break
print(count)
```
4. Classical question: a pair of rabbits give birth to a pair of rabbits every month from the third month after birth. The little rabbit grows to another pair of rabbits every month after the third month. If the rabbits don't die, what is the total number of rabbits every month?

```yue = int(input('Enter the number of months'))
for x in range(0,yue+1):
count = (yue-1)*2+(yue-2)*1+1
print(count)
```
5. Decompose a positive integer into prime factors. For example: enter 90 and print out 90=2x3x3x5.

```n = int(input('Please enter an integer'))
for x in range(2,n):
if n % x == 0:
print('x:',x)
n = n / x
```
6. A company uses a public telephone to transmit data. The data is a four digit integer and is encrypted in the transmission process. The encryption rules are as follows: add 5 to each number, then replace the number with the remainder of sum divided by 10, and then exchange the first and fourth bits, and the second and third bits. Find the encrypted value of the input four digit integer

```num = int(input('Please enter a four digit number'))
a = (num % 10 + 5) % 10 #Single digit
b = (num % 100 // 10 + 5)% 10 # tens
c = (num % 1000 // 100 + 5)% 10 # hundredths
d = (num // 1000 + 5)% 10 # thousand digits
print(d,c,b,a)
```
7. Decompose a positive integer into prime factors. For example: enter 90 and print out 90=2x3x3x5.

8. The principal of 10000 yuan is deposited in the bank with an annual interest rate of 3%. Every one year, add the principal and interest as the new principal. Calculate the principal obtained after 5 years.

```num = int(input('Please enter a four digit number'))
money = 10000
for x in range(1,num+1):
money *= 1.003
print(money)
```
9. Enter an integer and calculate the sum of its digits. (Note: the input integer can be any bit)

```num = int(input('Please enter a number'))
count = 1
for x in range(1,num+1):
if num // (10**x) != 0:
count += 1
else:
break
print(count)
```
10. Find the maximum common divisor and minimum common multiple of two numbers. (hint: the common divisor must be less than or equal to the smaller of the two numbers, and can be divided by the two numbers at the same time; the common multiple must be greater than or equal to the larger of the two numbers, and the multiple of the larger number can be divided by the decimal of the two numbers)

```a = int(input('Please enter a number'))
b = int(input('Please enter a number'))
count = 1
for x in range(2,a+b):
if a % x == 0 and b % x == 0:
break
print(x)
for y in range(2,a*b+1):
if y % a == 0 and y % b ==0:
break
print(y)
```

Keywords: Python

Added by jordan on Sun, 20 Feb 2022 19:47:28 +0200