basic.py
# Identifier: The name we use when we write the code. Named symbol.
# Project name
# Package name
# Module name.py python file name
# Specification: 1.It consists of alphanumeric underscores, but cannot start with a number.
#2. See the name
#3. Different letters and numbers are separated by underscores to improve your readability
#4. The keyword int ifwhile cannot be used
# Notes:#Single line comment ctrl+/
# Multi-line comments: three singles in pairs/Double quotes''' '''
# a=2#Assignment operation
# print(a)
# When you want to use a variable, make sure it has been defined and assigned.
# print(What you want to output)Output function output to the console
class_boolen.py
# Boolean bool boolean True False capitalize the first letter
# String: str Pairs of single quotation marks, double quotation marks, triple quotation marks, and the expanded content is a string
a='Qinghan'
b=0.02
# type(data) 判断data类型 int float str
print(type(a))
class_list.py
# List symbol[]
a=[1,0.02,'hello',[1,2,3],True]
#1: There can be an empty list a=[]
#2: The list can contain any type of data
#3: The elements in the list are separated by commas
print(len(a))
class_number.py
# Data type conditional statement loop statement
# Number: integer floating point
# Integer: keyword int
# a=10
# print(a*20)
# Floating-point number keyword float
# b=10.0
class_str.py
# Use of strings
# s='hello!'
# s=' '#Empty string
#1: Elements in a string: single letters, numbers, Chinese characters, and single symbols are all called an element.
# len(data)统计data的长度print(len(s))
#2: String value:String name[Index value]
# Index: Mark from 0
# print(s[5])
# print(s[-1])
# String takes multiple values: slice string name[Index head: Index tail: Step length]The default step size is 1#Take the head but not the tail
# print(s[2:4:2]#2
# print(s[:4]#0123
# print(s[3:])
# print(s[-1:-7:-1])
# # print(s[::-1])
# s=' hello!'
# Split string.split(Can specify cutting symbol,Number of cuts)
# Returns data of a list type, the child elements in the list are all string types
# The specified cutting symbol was cut away
# print(s.split("l",1))
# String replacement string.replace(Specify replacement value, new value, replacement times)
# s=' hello!'
# new=s.replace('o','@')
# print(new)
# s='666hello!666'
# Removal of specified characters from the string, string.strip(Specify characters)
#1: Remove spaces by default#2:Only the specified characters at the beginning and end can be removed
# print(len(s))
# new=s.strip("6")
# print(len(new))
# Concatenation of strings+Guarantee+The variable value types on the left and right sides should be the same
# s_1='Next job'
# s_2='Must be high salary'
# s_3=666#Integer str(digital)---Can be forced to the str type
# print(s_1+s_2+str(s_3))
# String formatted output% format
age=18
name='Qinghan'
score=99.99
# Formatted output 1:format features{}use this{}Lai Zhan Hang
# print("Beijing's{0},this year{1}Years old!".format(name,age))
# Formatted output 2:%%s string%d number%f floating point
print("Beijing's%s,this year%d years old!Took the exam%.2f"%(name,age,score))
# %s can fill in any data
# %d Only numbers (integer, floating point) can be filled in, only integer type is output
# %f can fill in numbers
class_dict.py
# Dictionary dict symbol{}Unordered curly braces
#1: Can be in empty dictionary a={}
#2: The way of data storage in the dictionary: key:value
#2: The value in the dictionary can contain any type of data
#3: The elements in the dictionary are separated by commas
# 4: The key in the dictionary must be unique
# a={"class":"python",
# " student":50,
# " age":20,
# " teacher":"girl",
# " score":[99,88.8,100]}
# Dictionary value: dictionary[key]
# print(a["score"])
# Delete pop(key)The key indicating the deleted value
# res=a.pop("teacher")
# print(res)
a={"class":"python","student":50,"age":20,"teacher":"girl","score":[99,88.8,100]}
# Add a[New key]=The key that does not exist in the value dictionary
# a["name"]="sun"
# print(a)
# Modify a[Existing key]=The existing key in the new value dictionary
# a["age"]=18
# print(a)
class_if.py
# Control statement branching and diverging loop statement forwhile
# Judgment statement if..elif..else keyword
# if conditional statement(Compare/logic/Member operations)
# 2: String tuple list dictionary empty data==False non-empty data==True
# 3 : Directly use Boolean values to control chicken ribs
# s='hello'
# if'O'in s:#When the statement following the if satisfies the condition and the result of the operation is True, its sub-statements will be executed
# print("Congratulations, you are an adult!")
#2 : A conditional statement can only have one if and one else, and no conditional statement can be added after it
# if conditional statement:
# Sub-statement
# else:Cannot add conditional statement
# Sub-statement
# age=20
# if age>=18:
# print("Congratulations, you are an adult")
# else:
# print("Come on")
#3 : If elif can be followed by conditional statements
# if conditional statement:
# Sub-statement
# elif conditional statement:
# Sub-statement
# else:Cannot add conditional statement
# Sub-statement
# input()The function gets a data from the console. The data obtained is all string types
age=int(input("Please enter your age:"))
# age=20if age >=18:print("Congratulations, you are an adult")
elif 18>age>0:print("Come on")else:print("Your age is entered incorrectly and cannot be a negative number")
class_list.py
# List symbol[]Brackets
# a=[1,0.02,'hello',[1,2,3],True]
#1: There can be an empty list a=[]
#2: The list can contain any type of data
#3: The elements in the list are separated by commas
#4: The elements in the list also have indexes, and the index value starts from 0
#5: Get a single value in the list: list[Index value]
# print(len(a))
#6 : The slice of the list is the same as the operation list name of the string[Index head: Index tail: Step length]
# print(a[0:5:2])
# When can we use the list?
# What is the list for? Storing data
# If the data you want to store is the same type, it is recommended to use a list
# How to add data to the list,Any type of data can be added
# append append append at the end can only add one at a time
# a=[1,0.02,'hello',[1,2,3],True]
# a.append("day")
# Insert insert data where you want to put it but specify the position-specify the index position of your element
# a.insert(2,"grass")
# Delete pop()
# a.pop()#Delete the last element by default
# a.remove(1) #Specify to delete a value
# a.pop(2)#Passing in the index value will delete the element at the specified index position
# res=a.pop()#The pop function will return the element that was deleted, the function return keyword
# print("the value of a list{0}".format(res))
# Modify a[Index value]=New value
a=[1,0.02,'hello',[1,2,3],True]
a[2]="star" #Assignment operation
print("the value of a list{0}".format(a))
class_operoter.py
# Operators in 5 categories
# Arithmetic Operator+-*/%
# Modular operation/The remainder operation determines whether a number is even or odd
# a=4
# print(a%2)
# Assignment operator=+=-=
# a=5 #Assignment operator
# a-=3#a+=1 is equivalent to a=a+1 a-=1 is equivalent to a=a-1
# print(a)
# Comparison operator>>=<<=!===6 comparisons
# The return value of the comparison result is the Boolean value True False
# a=10
# b=5
# print(a<b)
# print("get"!="GET")
# Logical operator and or extension: not
# The value returned by the logic operation result is the Boolean value True False
# And if the results on the left and right sides are both true, it will be true as long as one of them is false
# Really true and
# If the left and right sides of or are both false, it will be false as long as one is true.
# False false is false or
# a=10
# b=5
# print(a>11 and b>4)
# Member operator in not in
# The return value is also the Boolean value True False in both cases
s='hello'
l=[1,2,3]
d={"age":18,"name":"sunlight"}print("age"in d)#The dictionary is the judgment key
class_tuple.py
# Tuple symbol()Parentheses v
# a=(1,0.02,'hello',[1,2,3],True,(4,5,6))
# 1: There can be an empty tuple a=()
# 2: The tuple can contain any type of data print(type(a))
# 3: The elements in the tuple are separated by commas
# 4: The elements in the tuple are also indexed, and the index value starts from 0
# 5: Get a single value in a tuple: tuple[Index value]
# 6 : The slice of the tuple is the same as the operation tuple name of the string[Index head: Index tail: Step length]
# print(a[0:6:2])
# When operating the database, the conditions are stored
# Tuples do not support any modification (addition, deletion, modification)
# a[2]="sun"#TypeError:'tuple' object does not support item assignment
# a=(1,0.02,'hello',[1,2,3],True,(4,5,6))
#
# b=[1,0.02,'hello',[1,2,3],True,(4,5,6)]
# b[5]='hello'
# print(b)
# If your tuple has only one element, add a comma
# a=([1,2],)
# print(type(a))
class_for.py
# Loop forwhile keyword
# Python for loop syntax:
# for variable name in a certain data type: (data type includes: string list tuple dictionary collection, etc.)
# Code block
# in? Member operator in
# The number of for loops is determined by the number of data elements?
# s='hello'
# L=[1,2,3]
# d={"age":18,"name":'test'}#The data of the dictionary type is the key for traversal access
# for a in s:#The for loop traverses each element in s one by one, and then assigns it to a
# print(a)
# L=[5,6,9,3,7]
# Use the for loop to add all the data in the list
# sum=0#Store our sum
# for item in L:
# sum=sum+item
# sum=L[0]+L[1]+L[2]+L[3]+L[4]
# print("Sum of all values:{0}".format(sum))
# d={"age":18,"name":'test'}
# print(type(d.values()))#Get all value values in the dictionary
# print(d.keys())#Get all the key values in the dictionary
# for item in d:#Traverse is the key dictionary name[key]
# print(d[item])
# for item in d.values():
# print(item)
# The range function generates a sequence of integers
# range(m,n,k)m head n tail k step length is 1 by default, take head but not tail
# range(1,5,1)
# range(1,6,2)
# # Convenient to observe and convert the results into list form
# print(list(range(1,5,1)))
# print(list(range(1,6,2)))
# print(list(range(1,3)))
# print(list(range(8)))#The header defaults to 0, starting from 0
# for item inrange(3):# 012
# print("Cycles")
# L=[5,6,9,3,7]
# Please use a for loop to print out the value of each element in the list according to the index value of L
# #01234 range(5)
# for i inrange(5):# 01234
# print(L[i])
# Please use for loop and range function to complete 1-Add 100 integers (including 1, and 100)
sum=0#Storage and
for i inrange(1,101):#1-An integer of 100
sum=sum+i
print("Sum of all values:{0}".format(sum))
# For nested loops, please print out each element in the list separately
# L=[["Zhang San","Li Si","Wang Wu","river"],["sun","moon","Mountain river"]]
# for item in L:#Every time through the loop, get a sublist and assign it to item
# for a in item:
# print("The student’s name is:",a)
class_while.py
# name=["tata","Hua Hua","lala","hehe"]
# username=input("Please enter your name:")
# if username in name:#Member operator
# print("Username is correct")
# else:
# print("Username is incorrect")
# while control loop
# grammar:
# while conditional expression:#Logical members compare empty data((Refer to if statement) Boolean value
# Code block
# Execution law: first determine whether the conditional expression after while holds
# If it is True, execute the code block and continue to judge after the execution is complete---》If it is true, execute the code block and continue to judge after the execution is complete
# Otherwise do not enter the internal execution code block
# To prevent the code from entering an endless loop, add a variable to control the number of loops
# a=1#Initial value
#
# while a<=10:
# print("Now the input is the first{0}Times".format(a))
# print("Unable to end the loop")
# a=a+1
#
# sum=0#Sum initial value
# a=1#Start value of the loop
# while a<=100:
# sum=sum+a
# a = a +1
# print("The total sum is:",sum)
i=10
count=0while i>0:
sex=input("Please enter your gender:")if sex=='f':
i-=1#Number of inquiries minus 1
age=int(input("Please enter age:"))if10<=age<=12:print("Congratulations, you can join the football team")
count+=1else:print("Does not meet the joining conditions")else:print("Does not meet the joining conditions")
i-=1
#
# if i ==0:
# break#End the loop and jump out of the loop
# else:
# continue#End the current round and continue to the next round
python_function.py
# Python built-in functions
# print input len type str int float list range
# pop append insert keys split replace strip
# remove clear
# Summarize the characteristics of the function:
# Can be reused
# Function syntax.def keyword
# Function name naming convention: lowercase letters cannot be separated by underscores between letters that start with numbers and different
# def function name(Parameter 1, parameter 2, parameter 3):
# Function body: What function do you want this function to achieve?
# Call: function name()
# def qin_han(name='Fallen leaves'):#Formal parameters/Location parameter
# print("{0}Are ordinary people".format(name))
#
# # Call functions
# qin_han("English")
# qin_han()
# Use the range function to request any integer addition function and write it as a function
# The first step is to use code to implement the function. You can also select a set of data to prove whether your code is correct
# Step 2: Become a function plus def
# Step 3: Improve code reusability
def add_numbers(m,n,k=1):
sum =0for i inrange(m,n,k):
sum=sum+i
print("The sum value is:{0}".format(sum))add_numbers(1,5)#1234
Recommended Posts