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

[basic Python language] troubleshooting 2

編輯:Python

【python Language foundation 】 Difficult points sorting 1

The fifth chapter

  • stay python In the syntax , The statements in the loop body are not restricted , therefore , It can be any legal statement , Of course, it can also be a circular statement . In this way Nesting of loop statements .
  • while Loop statements and for Loop statements can take else Clause .
  • break sentence // continue sentence :break End the cycle ,continue End the current cycle

  •  pass Clever use of sentences

Chapter six  

6.1 character string  

  •   Indexes : The position of a character in a string can be identified by an index . The index number can start from the left , Using a digital 0、1、2、3、4、5、6…… To mark ; You can also start from the right , Use -1、-2、-3、-4、-5、-6…… To mark .

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

s

i

l

e

n

c

e

i

s

g

o

l

d

-15

-14

-13

-12

-11

-10

-9

-8

-7

-6

-5

-4

-3

-2

-1

  • Substring ( section ): A substring or slice is a sequence of consecutive characters in a string .
    • for example , In string “silence is gold” For example . Substring “silence”、“is”、“gold” The starting positions are 0、8 and 11, The end positions are 6、9 and 14.
    • Syntax format for accessing substrings : String variable name [m:n] 
    • function : Take from the string m Location -n Location string . That is, in position m Start , By location n-1 Ending string .

  • String connection :>>> “micro”+“computer” # formation “microcomputer”
  • Duplicate string :>>> "mom"*2                  # The result is 'mommom'
  • String detection :in / not in  # Return appears True, No return False
  • String comparison operation
  • Common functions of string

function

Example

value

Function description

len( character string )

len(‘Python Language ’)

8

Find the number of characters in the string

str( Numbers )

str(2.71828)

2.71828

Convert numbers to strings

chr( Encoding value )

chr(65)

A

Find the character corresponding to the encoded value

ord( character )

ord(“A”)

65

Find the code corresponding to the character  

  • String search and statistics

function

Function description

str1.find(subStr[,start[,end]])

Left to right search str1, return subStr stay str1 Index position of the first occurrence

str1.rfind(subStr[,start[,end]])

Right to left search str1, return subStr stay str1 Index position of the first occurrence

str1.index(subStr[,start[,end]])

Left to right search str1, return subStr stay str1 Index position of the first occurrence

str1.rindex(subStr[,start[,end]])

Right to left search str1, return subStr stay str1 Index position of the first occurrence

str1.count(subStr[,start[,end]])

Calculation subSt stay str1 Is the number of times

  •   String conversion methods

Method

Function description

str1.lower( )

The string str1 Convert to lowercase characters

str1.upper( )

The string str1 Convert to uppercase characters

str1.capitalize()

The string str1 title case , Other letters are lowercase

str1.title( )

The string str1 Capitalize each word in , Other letters are lowercase

str1.swapcase( )

The string str1 Case exchange of Chinese characters

  •   String segmentation and concatenation

Method

Function description

str1.split(sep=None,maxsplit=-1)

from str1 Start at left end , Use characters sep take str1 Split into multiple strings .maxsplit For the number of splits , The default value is -1.sep The default value is space .

str1.rsplit(sep=None,maxsplit=-1)

from str1 Start at the right end , Use characters sep take str1 Split into multiple strings .maxsplit For the number of splits , The default value is -1.sep The default value is space .

str1.join(iterable)

Connect multiple strings , And insert the specified character between adjacent strings .

  • Delete white space characters

Method

Function description

str1.strip(str2 )

Delete string str1 Blank characters at both ends or consecutive specified characters (str2 The characters in )

str1.rstrip(str2 )

Delete string str1 Trailing white space characters or consecutive specified characters (str2 The characters in )

str1.lstrip(str2 )

Delete string str1 A leading white space character or a continuous specified character (str2 The characters in )

Method

Function description

str1.startswith(str2 )

Judgment string str1 Whether to use the specified string str2 Start

str1.endswith(str2 )

Judgment string str1 Whether to use the specified string str2 end

  • replace() Method  :str1.startswith(str2, str3) The method is to use str3 To replace the string str1 Character or substring specified in str2 All occurrences of , Only one character or substring can be replaced at a time . 
>>> str1 = "Python: I like Python very much."
>>> str1
'Python: I like Python very much.'
>>> str1.replace("Python","Java")
'Java: I like Java very much.'

 6.2 list

>>> numint = [1,3 ,6 ,8, 9] # Composed of integers of the same type
>>> nums = [5, 8, 0, -9, 3.14, -2.7] # Composed of different types
>>> words = ['apple','classroom','school','www'] # Composed of strings
>>> score = [[70,80,90], 240] # Some elements are lists
>>> student = ['05191234', ' Zhang San ', 18, ' male ', False, 530.5, " Electronic information "]
# Composed of different types
>>> list0 = [ ] # An empty list
>>> list1 = [2.7] # There is only one element list
>>> lst = ['I','love','you'] # List encapsulation
>>> [a,b,c] = lst # list lst Unsealing , Assign the elements in the list to a,b,c
>>> a ='I'
>>> b ='love'
  • List creation
  • List operators
  • List update

usage

Function description

lst.append(item)

Put elements item Add to the end of the list

lst.insert(index, item)

Index in the list index Insert element before item

lst.extend(lst1)

In the list lst Tail insert list lst1 All elements of

lst.remove(item)

Remove the first element in the list item

lst.pop( )

Remove the last element in the list

del

Remove the index from the list index On the element

del

Remove entire list lst

lst[index] = item

Indexes index Replace the element on with the element item

>>> lst1 = [2,4,6,8]; lst2 = ['cat','dog']
>>> lst1.append(10)
>>> lst1=[2, 4, 6, 8, 10]
>>> lst2.insert(1,'goldfish')
>>> lst2=['cat', 'goldfish', 'dog']
>>> lst1.extend(lst2)
>>> lst1=[2, 4, 6, 8, 10, 'cat', 'goldfish', 'dog']
  • List of common functions and methods  

usage

Function description

len(lst)

Returns a list of lst The number of elements in

max(lst)

Returns a list of lst Maximum value of element in ( Elements must be of the same type )

min(lst)

Returns a list of lst The minimum value of the element in ( Elements must be of the same type )

sum(lst)

Returns a list of lst The sum of the elements in ( Element must be a number )

lst.count(item)

Returns a list of lst Medium element item Number of occurrences

lst.index(item)

Returns the element item In the list lst The first index in

lst.sort( )

Sort the list

lst.reverse( )

Sort the elements in the list in reverse order

sep.join(lst )

Use separator sep Put the string list lst It becomes a string

zip(lst1,lst2,…)

take lst1,lst2,… Elements in corresponding positions form tuples , Put it in zip In the object

enumerate(lst)

Enumeration list lst The elements in , Return enumeration object

 6.3 Tuples

Tuples can be thought of as lists with fixed values , Access to tuples is similar to a list . The main difference between tuples and lists is : Tuples cannot be modified directly . 

  • Lists are variable types , You can change the element values in the list at will , And adding and deleting elements in the list . Tuples are immutable types , Once the tuple is created , You can't change its contents .
  • Tuples are accessed and processed faster than lists .
  • Tuples can be used as keys for dictionaries . The list can never be used as a dictionary key .
  • Although tuples are immutable , The value of its element cannot be changed . however , If a tuple contains a variable sequence such as a list , The situation is slightly different . 

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