First time for python (Python beginning experience)

Keywords: Python

All the code written before was written in C/C + + language. Recently, I learned python, which is quite different from C/C + +, but it is also convenient. Let's make a simple record.

1. "hello world" in any language

a. First, if we want to be faster and use some Python statements, if the computer has configured the python environment, we can directly win+r, then enter cmd, then enter python, and then use print("hello world") to print the statements

b. The other is the most commonly used PyCharm

2. Some simple assignments of python

#We simply initialize a student's information
student_name="Xiao Ming"
student_number=3186004016
student_number1="3186004016"
student_age=22
#Print these types
print(type(student_name))
print(type(student_number))
print(type(student_number1))
print(type(student_age))

 

  You can see the difference between int and str (whether to add double quotation marks when assigning values), but if we enter a number from 0 to 9 from the keyboard, what type is it?

a=input("Please enter a number")
print("The number you entered is{}".format(a))
print("The type of this number is{}".format(type(a)))
a=int(a)
print("The converted type is{}".format(type(a)))

The operation results are as follows

  3. python implements a simple campus defense system

"""
This is a simple campus defense system
 1. Realize login function
 2. View administrator information
 3. View student list information
 4. Is the student in school
"""

Three double quotes can represent a multiline comment.

The following initializes a list of students, which can be accessed through subscripts

#Student list view, range left closed right open [0,4)
students = ["Xiao Ming", "Xiao Hong", "Xiao Gang","Xiao Wang"]
for i in range(0,4):
    print(students[i])
print("----------------------------------")
for s in students:
    print(s)

The operation results are as follows

  Then suppose that the four students are in school, and then we have to enter a student's name and judge whether he is in school or not

#Is the student in school
students = ["Xiao Ming", "Xiao Hong", "Xiao Gang","Xiao Wang"]
student_name=input("Please enter student name")
#Method 1
for i in range(0,4):
    if student_name==students[i]:
        print("Students are in school")
        break
else:
     print("Students are not in school")
#Method 2
if student_name in students:
    print("Students at school!!!!!")
else:
    print("Not a student")

Here's the overall implementation

# Campus defense system
# Author: Chen Zhilong
# Date: October 10, 2021
"""
This is a simple campus defense system
1,Realize login function
2,View administrator information
3,View student list information
4,Is the student in school
"""

# Initialize some data
ADMIN_ACCOUNT = "admin"
ADMIN_PASSWORD = "123456"
ADMIN_AGE = 22
students = ["Xiao Wang", "Xiao Gang", "Xiaoyu", "Xiao Ming"]

# Login function encapsulation function
def is_login():
    account = input("Please enter the account number")
    password = input("Please input a password")
    if ADMIN_ACCOUNT == account and ADMIN_PASSWORD == password:
        return True
    else:
        return False

#View student information list
def student_info():
    for s in  students:
        print(s)

#Is the student in school
def is_in_school():
    student_name = input("Please enter student name")
    if student_name in students:
        return True
    else:
        return False

print("Welcome to campus defense system")
menu=input("Please enter the action you want to perform: 1 means login and 2 means exit")
menu=int(menu)
if menu==1:
    print("The function you selected is login function")
    while True:
        if is_login():
            print("Login succeeded")
            print("Welcome, dear administrator:{}".format(ADMIN_ACCOUNT))
            while True:
                menu1 = input("Please enter the actions you want to perform: 1: view the student list 2: query whether the student is a student at school 3: exit")
                menu1 = int(menu1)
                if menu1 == 1:
                    student_info()
                elif menu1 == 2:
                    if is_in_school():
                        print("This student is a student at school!!!!")
                    else:
                        print("The student is not a student at school")
                elif menu1 == 3:
                    exit(0)
        else:
            print("Account password error")
            continue
elif menu==2:
    print("sign out")

4. Some special demonstrations of python

a. Custom print function

def my_print(*args):
    print(args)
    print(args[0])
    print(args[1])
    print(args[2])
    print(args[3])
    print(args[4])
my_print(1,2,3,"abc",5)

The operation results are shown in the figure

b. If it is two signals, it is similar to the map container in c + +, and exists as key value pairs

def my_print1(**args):
    print(args)
my_print1(port=123,ip="160.12.120.2",server_name="The server")

The operation results are shown in the figure

5. Function transfer problem

a. Immutable

def demo(n):
    n=666

a=1
print("Before calling")
print(a)
demo(a)
print("After call")
print(a)

Operation results

b. Variable

def demo(n):
    n[1]=666

l1=[1,2,3]
print("Before calling")
print(l1)
demo(l1)
print("After call")
print(l1)

  Operation results

6. Function can return multiple values

def demo1():
    return 1,9,"qqq"

i,j,str=demo1()
print(j,i,str)

The operation results are shown in the figure

Unfinished, to be continued

Posted by monkuar on Tue, 12 Oct 2021 00:13:07 -0700