Python learning notes strings, data types and loops

Multiline comments and multiline programs

  • When three quotation marks' msg 'are used to assign values to variables, they are multiline programs. If they are not assigned, they are multiline comments.
  • Single and double quotation marks can be used to assign values to variables, but there may be single quotation marks or double quotation marks inside the assignment, so you need to select quotation marks that are inconsistent with the outside.

pycharm use

  • IDE development environment

VIM classical linux Text editor below
Emacs than VIM Easier to use
Eclipse Java Of IDE,Support python,C,C++etc.
Visual Studio Windows Lower IDE,Microsoft development. Support python,C,C++etc.
notepad++
sublime python Developed

  • pycharm is an IDE mainly used for python development.

Character format output

  • placeholder

%s = string
%d = integer
%f = floating point

source code

    #__author:  Administrator
    #date:  2018-01-12

    name = input("Name:")
    age = input("age:")
    job = input("Job:")
    salary = input("Salary:")

    msg = '''
    ---------info of %s--------
    Name: %s
    Age : %d
    Job : %s
    Salary: %f
    ---------- end ----------
    ''' % (name ,name ,age ,job ,salary)

    print(msg)

Data operation

  • A preliminary understanding of data types

    1. number
      int (integer)

      integer
      Long integer
      py3 does not distinguish between integers and long integers

      Floating point float s handle numbers with decimals.
      complex with imaginary

    2. Boolean values have only two states

      True true
      False
      3. String
      >Everything in a computer is an object.
      >All things in the world are objects, and all objects can be classified.

  • list

Example

a = ["aiai","bibi","cici","didi","eiei"]  
# Add, delete, modify and check  
print(a[3])  #It takes "didi" and the first bit is "0".  
print(a[1:3]) # Slice left included, right not included. "bibi" and "cici"
print(a[1:])  # From "bibi" to the end
print(a[1:-1]) # From "bibi" to the penultimate value  
print(a[1:-1:2])  # 2 is the step size, which is taken from left to right. The values are "bibi" and "didi"
print(a[3::-2])  # Take 2 as the step, right to left, and the values are "didi" and "bibi"
print(a[-3::-1]) #From right to left, "cici", "bibi", "aiai"

a.append("fifi") #At the end of the list, add "fifi"
a.insert(1,"fifi") #Insert in the second position of the list, which can be adjusted at any time according to the position value  

a[1] = "gigi" #Replace "bibi" with "gigi"
a[1:3] = ["hihi","iiii"] #Replace "bibi" and "cici" with "hihi" and "III"

a.remove("aiai") #Delete "aiai"
a.pop(1)  #"bibi" can be returned after deleting "bibi" | variable assignment
del a[1] #Delete "aiai"
del a #Delete variable a  
a.clear() #empty  

Other actions for the list

Count: count the occurrence times of an element a.count("x")
Extend: extension list a.extend(b)
Index: query the location of an element, and find the location a.index("x") according to the content
Reverse: reverse a.reverse()
Sort sorts in the original position a.sort()

#index operation  
first_ci_index = a.index("cici")

first_list = a[first_ci_index+1:]

first_ei_index = first_list.index("eiei")

over_ce_index = first_ci_index + first_ei_index + 1

for loop

  • Loop = finite loop

Example 1

for i in range(1,101,2):   #2 is step size  
    print("loop:",i)  

Example 2

for i in range(100):  #Take 100
    if i < 50 or i > 70:  #If i is less than 50 or more than 70
        print(i)  #Print i  

Example 3

_user = "Popexp"
_passwd = "123456"  
for i in range(3):
    username = input("Username:")  
    password = input("Password:")
    if username == _user ande password == _passwd :
        print("Welcome %s login..." % _user)
        break #Jump out. The last else statement will not be executed after the for is interrupted.
    else:  
        print("Invalid username or password !") 
else: #As long as the above for loop is executed normally without interruption, else statement will be executed.
    print("Fuck off!")

while

Example 1

counter = 0
while True:
    if counter > 2**10
        break
    counter += 1  #counter = counter + 1  
    print("Infinite cycle")

Example 2

_user = "Popexp"
_passwd = "123456"

counter = 0
while counter < 3:

    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _passwd:
        print("Welcome %s login..." % _user)
     break  # Jump out, interrupt
    else:
        print("Invalid username or password !")
    counter += 1
else:
    print("Fuck off!")

Instance 3 ා infinite loop. You can exit if you want to.

_user = "Popexp"
_passwd = "123456"

counter = 0
while counter < 3:
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _passwd:
        print("Welcome %s login..." % _user)
     break  # Jump out, interrupt
    else:
        print("Invalid username or password !")
    counter += 1
    if counter == 3
    keep_going_choice = input("continue?[y/n]")
    if keep_going_choice == y:
        counter = 0
else:
    print("Fuck off!")

break,continue

  • break

  • continue

Example

exit_flag = False
for i in range(10):
    if i < 5:
        continue  #End this cycle and continue the next cycle.
    print(i)  
    for j in range(10):
        print(j)
        if j==6:
            exit_flag = True
            break #Jump out of current loop
    if exit_flag:
        break  

Tags: Python Pycharm vim Linux

Posted on Thu, 30 Apr 2020 17:16:15 -0700 by culprit