python for while loop data type built-in method

while condition:
The sub code block executed by the loop after the condition is established

Each time the subroutine of the loop body is executed, it will re judge whether the condition is true
If yes, continue to execute the subcode. If not, exit

break is used to end the loop of this layer

###1: continue: end this cycle and start the next cycle

1. Directly let the code return to the condition judgment of the while loop and re judge that the condition is executing. This is called continue

  • break end this layer cycle # continue end this cycle
  • continue will make the loop body code return to the condition judgment directly to re judge

### 2.while+else

When the while loop is not broken artificially, the else / execution subroutine will be executed

When the while loop is artificially broken, the else / execution sub code will not be executed

3. Dead cycle

"" "dead cycles can make the CPU extremely busy or even collapse" ""

###4.for loop

1. Whatever the or loop can do, the while loop can do
However, the for loop syntax is more concise and more convenient in the problem of loop value

Now I have a class list, in which several people's names are saved. Cycle out each element of the list/Data, print it out
 Loop out each element of the list and print it

while implementation:

  • For variable name in iteratable object: # string, list, dictionary, tuple, set, for loop body code
  • ps: variable name. If there is no suitable name, you can use i,j,k,v,item, etc
  • for loop implementation:
    for variable name in iteratable object
    The loop takes out the elements in the list in turn until there is no left in the list.
Now I have a class list, in which several people's names are saved. Cycle out each element of the list/Data, print it out
 Loop out each element of the list and print it
 for Loop can print out all the elements in the list

  • for loop string
From the beginning, assign the first character to the variable name i,Print i Then the subcode ends,
After this i If you are liberated, you will not bind this i When you are ready, bind the second letter e,Then bind l,Print l,This is called for Loop, iterative value.

  • for loop dictionary
detailed

for circular Dictionary: only k can be obtained by default

5.range keyword

  • while realizes circular printing of 0-1000

    Keyword range
  • Range can produce a range of numbers
range It's special cooperation for The frequency of recycling is very high
range That's a range of numbers,

6. Function of for loop

  • for+break
    break: the function is also used to end the loop of this layer

  • for+continue
    continue: this function is also used to end this cycle

  • for+else
    else: it is only executed when the for loop ends normally

7. Nested use of for loops

  • for loop nesting (large loop and small loop)

    1.for nesting
    Demo 1:

    Demo 2:
  • for loop (99 multiplication table)

###8. Built in method of data type

In daily life, different types of data have different functions
# eg: the table data file has the functions of processing tables (PivotTable graphical formula calculation)
The video data file has various functions such as fast forward and acceleration

There are also different data types in the program, and each data type also has its unique functions
## Built in methods for data types
 The effect of method in the code is       name()     For example: print() inpit() id()etc.


# Integer (int)
i = 999
# The only thing you need to master is type conversion
res = '123'               # '123' type string
print(type(res))         # View res type as (string)
res = int(res)           # Convert res to (integer)
print(type(res))         # View res type as (integer)
# int can only convert pure numbers when doing type conversion
int('123.123')        # Error reporting integers do not recognize small numbers
int('jason123')      # Error reporting does not recognize data other than numbers

# # int can also do hexadecimal conversion
print(bin(100))     # Convert decimal 100 to binary 0 b1100100          #Decimal every decimal one
print(oct(100))     # Convert decimal 100 to octal 0 o144             #Octal every eight into one
print(hex(100))     # Convert decimal 100 to hexadecimal 0 x64             #Hexadecimal every hexadecimal into one

# 0b starts with binary number 0o starts with octal number 0x starts with hexadecimal number
# How to convert other base numbers to decimal
print(int('0b1100100', 2))     # 100               # Preceded by int, followed by string form, followed by hexadecimal number
print(int('0o144', 8))         # 100
print(int('0x64', 16))         # 100

# Mutual type conversion

*1. Floating point
Type conversion:

res = '123.23'
print(type(res))      # class str input and output are strings
res = float(res)     # Convert the value pointed to by res to floating-point type and re assign it to the res variable name
print(type(res))     # The query res type is floating point

print(float('123'))   # Convert integer to floating point 123.0

print(float('jason123'))   # The error report does not recognize the error outside the small character point of the number

  • 2. String str
    Type conversion
# str of string
# Type conversion

# *Any data type can be converted to a string
print(str(123))                            # Turn integer
print(str(123.21))                        # To floating point
print(str([1, 2, 3, 4]))                  # Transfer list
print(str({'name': 'jason', 'pwd': 123}))  # Turn dictionary
print(str((1, 2, 3, 4)))                  # Tuple
print(str(True))                        # To Boolean
print(str({1, 2, 3, 4}))                # Turn set

# All converted to strings

9. Basic usage:

res = 'hello world'
# # Index value
# print(res[1])        # e
#
# # Slicing operation           # Head and tail
# print(res[1:4])      # ell

# Step operation
print(res[1:10:1])      # ello worl   # Writing 1 is the same as not writing 1. 1 means not jumping, and 2 means jumping 1 grid
print(res[1:10:1])      # ello worl
print(res[1:10:2])     # el          # 2: Jump one space and take one space after another

# Help you pick up regularly, jump a few squares, jump a few squares.

res = 'hello world!'
1. The index supports negative numbers
print(res[-1]) #! Take the last digit
2. Slice supports negative numbers
Print (RES [- 5: - 1]) # world takes care of the head and ignores the tail
3. The step size supports negative numbers
print(res[-5 👎- 1] ) # cannot get direction conflict

5. Count the number of characters in the string

res = 'hello world!'
Print (len (RES)) # 12 # counts the number of spaces, and Len counts the number of characters. If you put what you want in it, the number of cells of this data will be returned

6. Remove the character strip() specified at the beginning and end of the string

# name = '  jason  '                  # Two spaces at the beginning and end
# print(name, len(name))             # Print name and count the number of 9 names
# print(len(name.strip()))          # Statistics show that name has 9 numbers. strip() moves out the leading and trailing spaces by default
# # You can also specify a move out symbol
# name1 = '$$jason$$'
# print(name1.strip('$'))           # Specifies the characters to move out of the beginning and end
# print(name1.lstrip('$'))          # lstrip remove left
# print(name1.rstrip('$'))          # rstrip remove right

  • When adding spaces when user input, remove the leading and trailing spaces

    eg: strip after you get used to it, you can directly:

username = input('Please enter your name '). strip()

Meaning: first obtain the content entered by the user, remove the spaces at the beginning and end, assign the variable name username, and use.

7. Cut the string split() according to the specified character. The result of this method is a list

res2 = 'jason|123|18'
# This string contains jason's user name, password and age. Now assign these three values to three variables.
print(res2.split('|'))      # The cutting result is a list ['jason ',' 123 ',' 18 ']
print(res2.split('|', maxsplit=1))    # It is used to control the number of cuts. The number of cuts is 1. Only one is cut, from left to right
print(res2.rsplit('|', maxsplit=1))   # Control the cutting times, and the cutting is 1: rsplit cutting from right to left


whole

res2 = res2.split('|')
name = res2[0]
gae = res2[1]
count = res2[2]
print(name, gae, count)


skill:

How to view data types and what are the built-in methods
Period (.)

Added by misterm on Mon, 08 Nov 2021 17:44:46 +0200