day5 ternary operator while loop summary and operation

Posted by kevinsequeira on Sun, 20 Feb 2022 20:03:18 +0100

Summary of while loop of ternary operator

1, Ternary operator

Ternary operator of python

• Syntax:
```Value 1 if expression else Value 2
```
• Operation rule: if the result of the expression is True, the result of the whole operation is the value 1, otherwise it is the value 2
```# Case: if a is greater than 10, let a add 1, otherwise let a subtract 1 (use the trinomial operator)
a = 18
result = a + 1 if a > 10 else a - 1
print(result)
```

2, while loop

1.while loop

• Syntax:
```while Conditional statement:
Circulatory body
```
• explain:

while - keyword; Fixed writing

Conditional statement - can be any resulting expression (assignment statement)

: - Fixed writing

Loop body - one or more statements that maintain an indent with while; The loop body is the code that will be executed repeatedly by flash

• Execution process

First judge whether the conditional statement is True. If so, execute the loop body; After executing the loop body, judge whether the conditional statement is True. If so, execute the loop body again... And so on. If the conditional statement is false, the loop ends.

```#Case 1: use while to print hello word five times!
n = 0
while n < 5:
n += 1
print('hello word!')
```
```#Case 2: enter the login password until it is entered correctly
```

2. Selection of for and while

If the number of cycles is determined, use the for loop; If the number of cycles is uncertain, use while

(use for for all problems that can be solved with for, and use while only for those that cannot be solved)

3, Circular keyword

1.continue and break

• continue

Function: end a cycle (if continue is encountered when executing the cycle body, when the cycle ends, directly enter the next cycle)

```#Case 1: printing three rows of aaa
for x in range(3):
print('aaa')
continue
print('bbb')
```
```#Case 2: printing odd numbers within 0-99
for x in range(100):
if x % 2 == 0:
continue
print(x)
```
• break

Function: end the whole loop (if you encounter a break when executing the loop body, the whole loop will end directly)

```#Case 1: print a row of aaa
for x in range(3):
print('aaa')
break
print('bbb')
```

while encountering break:

```while True:
Actions that need to be repeated
if Conditions for the end of the cycle:
break
```
```#Case: randomly generate a random number from 0 to 100. The player inputs the number. The input number is equal to the generated number. The game is over! If it is not equal, give a prompt of 'big' or 'small'
from random import randint
num = randint(0,100)
c = 0
while True:
n = int(input('Please enter a number:'))
c += 1
if n > num:
print('Big!')
elif n < num:
print('Small!')
else:
break
print('Congratulations, you guessed right!Guess together',c,'second')
```

4, else keyword

1. Complete cycle structure

• Complete for:
```for variable in Sequence:
Circulatory body
else:
Code snippet
```
• Complete while:
```while Conditional statement:
Circulatory body
else:
Code snippet
```
• The existence of else will not affect the execution of the original loop
• The code after else will be executed after the end of the loop (if the loop ends because of a break, it will not be executed)
```#Case: judge whether the string is a pure number
str = '3465475'
for x in str:
if not '0' <= x <= '9':
print(str, 'Not a pure numeric string')
break
else:
print(str, 'Is a pure numeric string')
```

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

```count = 0
for i in range(100, 201):
for j in range(2,i-1):
if i % j == 0:
break
else:
count += 1
print(i)
print(count)
```
2. Find the cumulative value of integers 1 ~ 100, but it is required to skip all numbers with 3 bits.

```sum = 0
for i in range(1,101):
if i % 10 != 3:
sum += i
print(sum)
```
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 = 2
b = 1
for i in range(20):
a,b = a + b,a
print(a,'/',b)
```
4. Write a program to calculate the factorial n of n! Results

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

```num = 1
sum = 0
for i in range(1,21):
num *= i
sum +=num
print(sum)
```
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 a Value of:'))
n = int(input('Please enter n Value of:'))
sum = 0
m = a
for i in range(1,n+1):
sum += a
a = a * 10 + m
print(sum)
```
7. Console output triangle

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

n = int(input('Please enter an integer:'))
for i in range(n,0,-1):
print('*' * n)
n -= 1

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

n = int(input('Please enter an odd number:'))
for i in range(1,n + 1,2):
m = int((n - i)/ 2 )
print(' ' * m,'*' * i)

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('Please enter an integer:'))
c = n
for i in range(1, n + 1):
for l in range(1, n - i + 1):
print(' ',end='')
for j in range(1,i + 1):
print(j,end='')
for k in range(j - 1, 0, -1):
print(k, end='')
print('')
```
8. 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(7):
for b in range(21):
for c in range(51):
if 15 * a + 5 * b + 2 * c == 100:
print(a,b,c)
```
9. 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)?

```n = 1
while True:
n += 1
if 0.08 * 0.001 * 2 ** n >= 8848.13:
print(n)
break
```
10. 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?

```a = b = 1
c = 0
n = int(input('Please enter the month:'))
if n == 1 or n == 2:
print('1')
else:
i = 2
while i < n:
c = a + b
a = b
b = c
i += 1
print(c)
```
11. Decompose a positive integer into prime factors. For example: enter 90 and print out 90=2x3x3x5.

```a = int(input('Please enter a positive integer:'))
i = 2
num = a
print(a, '=', end='',sep='')
while True:
if num % i == 0:
num = num // i
print(i,'x',end='',sep='')
i = 1
if num // 2 == i:
print(num)
break
i += 1
```
12. 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 integer:'))
ge = (num % 10 + 5) % 10
shi = (num // 10 % 10 + 5) % 10
bai = (num // 100 % 10 + 5) % 10
qian = (num // 1000 + 5) % 10
print(qian,bai,shi,ge)
```
13. Decompose a positive integer into prime factors. For example: enter 90 and print out 90=2x3x3x5.

```a = int(input('Please enter a positive integer:'))
i = 2
num = a
print(a, '=', end='',sep='')
while True:
if num % i == 0:
num = num // i
print(i,'x',end='',sep='')
i = 1
if num // 2 == i:
print(num)
break
i += 1
```
14. 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.

```sum = 0
a = 10000
for i in range(1,6):
b = a * (1 + 0.003) ** i
sum += b
print(sum)
```
15. Enter an integer and calculate the sum of its digits. (Note: the input integer can be any bit)

```sum = 0
m = 1
a = 1
n = int(input('Please enter an integer:'))
for i in range(1,n + 1):
a = n // 10 ** (m - 1) % 10
m += 1
sum += a
print(sum)
```
16. 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)

```m = int(input('Please enter m Value of:'))
n = int(input('Please enter n Value of:'))
min = m if n > m else n
for i in range(2,n * m + 1):
if i % m == 0 and i % n == 0:
break
for x in range(1,min + 1):
if m % x == 0 and n % x == 0:
a = x
print('Least common multiple:',i)
print('Maximum common factor:',a)
```

Topics: Python