程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Try python (I)

編輯:Python

Small test Python( One )

Personal blog : Small test Python( One )

Preliminary study Python.

Introduce

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 .

first Python Program

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!")

Variable

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"

Input

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))

Array ( list )

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))

Tuples

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])

function

keyword def start


def add(num1, num2):
result = num1 + num2
return result
def display():
print("Hello World!")
print(add(1, 1))
display()

Under controlled conditions (if)

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

Dictionaries

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

while loop

The principle is basically the same as that of other languages

Examples are as follows :

 i = 1
while i <= 10:
print(i)
i += 1

for loop

amount to Java The enhancement of for loop .

Traversal array example :

 number = [1, 2, 3, 4, 5, 7]
for n in number:
print(n)

result :

collocation range() function

  1. There is only one parameter n: Traverse [0,n), barring n

for index in range(6):
print(index)

result :

  1. There are two parameters , The first parameter is zero m, The second parameter is n: Traverse [m,n).

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved