Personal blog : Small test Python( One )
Preliminary study Python.
Python It's an interpretive type 、 object-oriented 、 High level programming language for dynamic data types .
Official announcement ,2020 year 1 month 1 Japan , stop it Python2 Update .
Python2.7 It's the last one Python2.x edition .
Python Unlike C++、Java equally , Main function is required , You don't need a semicolon after the statement , function 、 Under controlled conditions 、 Classes and so on do not need to have "{}“ encase , But you need to indent , Having indentation is equivalent to adding ”{}", The indentation of the assignment statement will make an error .
print("Hello World!")
Similar to other languages , There is no need to emphasize types when defining , Direct assignment
name = "John"
num1 = 10
num2 = 20
print(name + ":" + str(num1 + num2))
in addition ,Python There is no self increase in "i++“ Self decrement "i–”, But there is "i += 1"
The default input is string , You need an integer 、 Floating point type needs to be converted ,
Strings can be used directly "+" Connect
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
result = int(num1) + int(num2)
print("Result is: " + str(result))
Subscripts can be negative numbers ,-1 Represents the last element ,-2 For the second from the bottom …, Neither positive nor negative numbers can exceed the range of the array
Take part :
num[1:]: Output subscript 1 And subscript ratio 1 All large array elements
num[1:4]: The output subscript range is [1, 4) The elements of , Be careful : barring 4
num = [1, 2, 3, 4, 5, 6]
print(num)
print(num[1])
print(num[-2])
print(num[1:])
print(num[1:4])
print(len(num))
Similar to arrays , The element of a tuple cannot be modified , Use parentheses
tuples = (1, 2, 3, 4, 5, 6)
print(tuples)
print(tuples[1])
print(tuples[1:])
print(tuples[1:4])
keyword def start
def add(num1, num2):
result = num1 + num2
return result
def display():
print("Hello World!")
print(add(1, 1))
display()
Python There is no "&&“ and ”||", either "if(!0)“ This usage , There are similar logical operators "and”,“or”,“not”.
test = True
if test and False:
print(1)
elif False or 0:
print(2)
elif not(0):
print(3)
else:
print(4)
result : Fine products
Each key value pair of the dictionary key=>value Divide with colon , The whole dictionary is enclosed in curly braces , Can store objects of the type of thermal kindness
dictionary = {
1: "one",
2: "two",
"o": 1,
"t":"test"
}
print(dictionary[1])
print(dictionary["t"])
print(dictionary.get("o"))
print()
print(dictionary.get(5)) # use get() If you can't find it, you won't report an error , When there is only one parameter , No return found "None"
print(dictionary.get(5, "You can't find it")) # For two parameters , The second parameter returned cannot be found
The principle is basically the same as that of other languages
Examples are as follows :
i = 1
while i <= 10:
print(i)
i += 1
amount to Java The enhancement of for loop .
number = [1, 2, 3, 4, 5, 7]
for n in number:
print(n)
result :
for index in range(6):
print(index)
result :
Use range() Function traverses array : First use len() Function to get the array length , Then the array subscript can be obtained by using the above method [0,n).
number = [1, 2, 3, 4, 5, 7]
for index in range(len(number)):
print(number[index])
The result is the same as the above method of traversing the array .
Study :Youtube
Mike Dane