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

Python3 rookie learning notes 1

編輯:Python

One 、python3 Basic grammar

1、 notes

Single-line comments : With # start

Multiline comment : Use multiple # Number

Start and end of multiline use ''' perhaps """

2、 Indent

Statements in the same code block must contain the same number of indented spaces

The number of spaces in the indent number is inconsistent , Can cause a run error

3、 Multi line statement

Python It's usually a sentence written one line at a time , But if the sentence is long , We can use backslash \ To implement multiline statements

In addition : stay [], {}, or () Multiple lines in , No need to use backslash \;

Backslash can be used to escape , Use r You can keep the backslash from escaping . Such as r"this is a line with \n" be \n Will be displayed , It's not a new line

4、 Show multiple statements on the same line , Use ; separate

Two 、 Basic data type

1、Python3 There are six standard data types in

  • Number( Numbers ): int、float、bool、complex( The plural )
  • String( character string )
  • List( list )
  • Tuple( Tuples )
  • Set( aggregate )
  • Dictionary( Dictionaries )

In addition

  • Immutable data (3 individual ):Number( Numbers )、String( character string )、Tuple( Tuples );
  • Variable data (3 individual ):List( list )、Dictionary( Dictionaries )、Set( aggregate ).

2、Number Numeric type

2.1、Number Object creation and deletion

        When you specify a value ,Number The object will be created :var1=1

         By using del Statement to delete a single or multiple object :del var1,var2

2.2、 Correlation function

 type() Function can be used to query the object type of variable

You can also use isinstance To determine the type of data , The return value is TRUE perhaps FALSE

The difference between the two is as follows :

  • type() A subclass is not considered a superclass type .
  • isinstance() Think of a subclass as a superclass type .

Be careful :Python3 in ,bool yes int Subclasses of ,True and False Can be added to numbers , True==1、False==0  Returns the  True, But it can go through  is  To judge the type .

2.3、 Numerical operation

        2 / 4  # division , Get a floating point number
        0.5
        >>> 2 // 4 # division , Get an integer
        0

        >>> 17 % 3 # Remainder
        2
        >>> 2 ** 5 # chengfang
        32

Be careful :

  • 1、Python Multiple variables can be assigned at the same time , Such as a, b = 1, 2.
  • 2、 A variable can be assigned to different types of objects .
  • 3、 The division of a number consists of two operators :/  Returns a floating point number ,//  Returns an integer .
  • 4、 In mixed calculation ,Python Will convert integer to floating point

3、String String type

Python Use single quotes for strings in  '  Or double quotes  "  Cover up , Use the backslash at the same time  \  Escapes special characters .

Be careful :

  • 1、 Backslash can be used to escape , Use r You can keep the backslash from escaping .
  • 2、 String can be used + Operators join together , use * Operator repetition .
  • 3、Python There are two ways to index strings in , From left to right 0 Start , From right to left -1 Start .
  • 4、Python String in cannot be changed .Python String cannot be changed . Assign a value to an index location , such as word[0] = 'm' Can cause errors

4、List( list )

The types of elements in a list can vary , It supports Numbers , Strings can even contain lists ( The so-called nested )

The list is written in square brackets  []  Between 、 Comma separated list of elements

Just like a string , The list can also be indexed and intercepted , After the list is truncated, a new list containing the required elements is returned

Be careful :

  • 1、List Write between square brackets , The elements are separated by commas .
  • 2、 Just like a string ,list Can be indexed and intercepted (Python List interception can receive the third parameter , The parameter function is the step size of interception , If the third parameter is negative, it means reverse reading )
  • 3、List have access to + Operators are spliced .
  • 4、List The elements in can be changed .


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