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

Python basic syntax

編輯:Python

Python identifier

stay Python in , The identifier has letters 、 Numbers 、 Underline composition .

stay Python in , All identifiers can include English 、 Numbers and underscores (_), But don't start with a number .

Python Identifiers in are case sensitive .

Identifiers that begin with underscores have a special meaning . Start with a single underline _foo Represents class properties that cannot be accessed directly , It needs to be accessed through the interface provided by the class , Out-of-service from xxx import * And import ;

Starting with a double underscore foo Represents a private member of a class ; Beginning and ending with double underscores foo representative Python A special way of marking , Such as init__() Constructor representing class .

Python There are five standard data types

Numbers( Numbers )
String( character string )
List( list )
Tuple( Tuples )
Dictionary( Dictionaries )

Python Four different types of numbers are supported :

int( signed int )
long( Long integer [ It can also represent octal and hexadecimal ])
float( floating-point )
complex( The plural )

python The list of strings for has 2 The order of values is :

Left to right index default 0 At the beginning , The maximum range is less string length 1
Right to left index default -1 At the beginning , The maximum range is at the beginning of the string
List( list ) yes Python The most frequently used data type in .

List can complete the data structure implementation of most collection classes . It supports characters , Numbers , Strings can even contain lists ( That is, nesting ).
List with [ ] identification , yes python The most common composite data type .

The cutting of the values in the list can also use variables [ Header subscript : Tail subscript ] , You can intercept the corresponding list , Left to right index default 0 Start , Right to left index default -1 Start , The subscript can be empty to indicate that the head or tail is taken .
plus + Is the list join operator , asterisk * Is a repeat operation .
Tuples are another data type , Be similar to List( list ).

Tuple use ”()” identification . The inner elements are separated by commas . But tuples cannot be assigned twice , Equivalent to read-only list .
Dictionaries (dictionary) Except for the list python The most flexible type of built-in data structure .

A list is an ordered combination of objects , A dictionary is an unordered collection of objects . The difference between the two is : The elements in the dictionary are accessed by keys , Instead of accessing by offset .
Dictionary use ”{ }” identification . The dictionary is indexed by (key) The value corresponding to it value form

Variables and simple data types

1. Variable names can only contain letters 、 Numbers and underscores . It can start with letters and underscores , But don't start with a number ; Variable names cannot contain spaces

2. data type -- character string

Strings can be enclosed in double or single quotation marks , Both work the same . What kind of , It depends on the contents of the string , If the string contains single quotation marks , Then use double quotation marks outside ; If the string contains double quotation marks , Then use single quotation marks .

3. String function

title()-- Show each word in uppercase

title.upper()-- All capitals

title.lower()-- All lowercase

4. String concatenation -- use "+" Number

a="1"

b="2"

print(“1+2”)

Output 1+2

print(1+2)

Output value 3

Annotation symbols

Single-line comments --#

Multiline comment --"""……"""

list

Put similar things in an array

string = ['abc' , 'bcd' , 'efg']

print(string)

Output : ['abc' , 'bcd' , 'efg']

You can also define an empty list directly string[]

Access list elements

string[0], You can access the first element

Access the last element --string[-1]

Modifying elements

string[1] = 'apple'

Additive elements

Add... At the end --string.append('student')

5. Insert elements

string.insert(0,'banana'), be banana Inserted at the beginning of the list , The other elements move back

6. Remove elements

del string[0]

Use del Deleted elements , Will not be able to access again

string2 = string.pop()

pop Function removed string The element at the end , And assign it to string2

If you want to delete the element of the specified position , just string.pop(2)

Delete elements... Based on values

string.remove('apple')

remove Delete only the first specified value , If you want to delete all matching values in the list , Circulation is required
 


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