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

python

編輯:Python

Programming based

Shortcut key

open settings:ctrl+alt+S
Run current code :ctrl+shift+f10
Run the current script :shift+f10

lookup :ctrl+f
Replace :ctrl+R
Global search :ctrl+shift+f
Global replacement :ctrl+sshift+r
revoke :ctrl+z
Counter cancellation :ctrl+shift+z
Indent :tab
Reverse indent :shift+tab
Page turning :pageup and pagedown
Head of line :home
At the end of the line :end

Quick correction :alt+enter
Quick annotation :ctrl+/
Copy code :ctrl+d
Delete the code :ctrl+y
Copy code :ctrl+o
Select a word or code block :ctrl+W
Find documents quickly :ctrl+q
Module or project rename :shift+f6
Insert down :shift+enter
Insert up :ctrl+alt+enter

View view :alt+1
Look at the structure view :alt+7

Quick access code :ctrl+b
Quick view of history :alt+ Left and right
Fast switch :alt+ Up and down

Switch views :ctrl+tab
View the resource file : two shift
See where the method is called :ctrl+alt+h
Look at the parent class :ctrl+u
View inheritance relationships :ctrl+h

1.1 Variable

1.1.1 Output and input

input() What's the function for , What is the return value ,int() What do you do? , What is the return value

input() The function is used to input data , The string in parentheses is a prompt , It is the string that is output before the input data .
input() The value returned by the function defaults to the string type

int() Function first converts a string to an integer

Several rules for variable naming :

Letter 、 Numbers and underline , And the first character cannot be a number .
Special words cannot be used as variable names
A variable name corresponds to a variable , No two variables have the same variable name

Simple variable types :

python in , There is no rounding , Rounding refers to discarding the part after the decimal point of the original value

print(num1,num2)
A box has more than one grid , Each item can hold one item . bring print(num1) Join in print(num2) Just use one of the grids to carry things each time , So it needs two times of handling .
and print(num1,num2) It is to use two lattices to pack items at the same time , It can be done in one time .
print A function is a box with an infinite number of boxes .

stay python in , A string of characters between two single quotes or two double quotes is a string . It should be noted that , If you use single quotation marks to represent a string , Then the string cannot contain an apostrophe
text1 = “I‘m a programmer.”
text2 = ‘i’m a programmer.’// No way

1.1.3 Numerical calculation

+,-,*,/

python3 in , If the result of dividing an integer is not an integer ,python The result of the operation will be stored as floating-point white energy

pow() Find a number n Power
pow(x,y,z)
without z,x Of y Power
If you enter ,x Of y The power divided by z The remainder of ,x**y%z
% Remainder

abds Used to find the absolute value
sqrt For finding the square root

1.1.4 An operation

Bitwise AND &
Press bit or |
bitwise exclusive or operator ^
The corresponding digits are different 1, Same as 0
Move left <<
Move right >>
a<<2 :a The binary digits of the whole move to the left by two digits, and then convert back to the decimal number .
If you move left , Then the covered high position disappears , And the vacant position is 0 Not up to .
If you move to the right , Low order hours , The high level becomes 0

1.1.5 Use string

String splicing

use + Number
If you want to add a number to a string , These numbers must also be variables of type string
lower Replace all English characters with lowercase
upper All English characters are capitalized

capitalize: The first alphabetic character of the string becomes uppercase , If the first character is not a letter, it does not change
title Its function is to capitalize the first letter of each word in the string , When words are separated by characters other than letters , Such as good8day, Turn into Good9Day

Escape character
\ Line continuation operator
\ Backslash notation
’ Single quotation marks
" Double quotes
\a Ring the bell
\b Backspace
\e escape
\000 empty
\v Vertical tabs
\r enter

Repeat output string
python This word outputs 3 Time

text = “Python\n”
print(text*3)

Write notes
ctrl+/
perhaps #

1.2 Three structures

1.2.1 Loop structure

1.2.2 Branching structure

1.2.3 conditional

python cognizance 0 perhaps null by False, Arbitrary non 0 Value or any non null value is TRUE,
Whether it is equal or not :==
It doesn't wait :!=

1.2.4 Application branch structure

Colon sign if The end of judgment
if The code indented below is in true To be operated under
Indenting the same code is called a code block
tab once , Or space around

1.2.5 Loop structure

python No, do``while
python There are only two cycles in :while Statement and for sentence

1.2.6 continue and break

arr = [1,2,3,4,5,6,7,8,9,10]
for i in range(len(10)):
if arr[i]%5 == 0:
continue
print(arr[i])



1.3 list

1.3.1 Definition list

List in brackets

1.3.2 Operate on elements

The elements in the list are the same as variables , Values can be updated at any time , It can also be used to assign values to other variables .
The elements in the list can be changed
append and Insert These two built-in list Hannah trees
Built in functions append The function of is to insert new elements at the end of the list , The function insert The function of is to insert a new element at the specified index , All the elements behind the element machine originally at this index position are moved backward as .

Remove the elements from the list , There are two functions to choose from :remove and pop.remove The standard format of the function is :list.remove(obj), Removes the first occurrence of a value in the specified list .
pop The standard format of the function is :list.pop(index), Removes an element at an index position in the specified list , It also returns the value of the removed element .

notes

A single :#
Multiple lines : Three single quotes , Three double quotes

Statement wrap

Backslash to implement multiline
total = item_one +
item_two +
item_three

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

Variable type

Immutable data :Number、String、Tuple
Variable data :List、Dictionary、Set

Simple numeric type

integer

Positive or negative integers , Without decimal point ,Python3 No, Long type

floating-point

Integers + decimal
2.5e2=250

Boolean type

true =1
false = 0
Can be added to numbers

Plural type

a+bj
complex(a,b)
a,b All are floating point type

Digital type conversion

int()
float()
complex(x): take x Convert to a plural type , The real part is x, The imaginary part is 0
complex(x,y)

Operator

+、-、*、/、%、
power ( * *)
//: Divide and conquer : Return to the business certificate section
9//2 = 4
9.0/2.0 = 4.0

Assignment operator

Variable = expression

Compound assignment operator

=
+=:c+=a equivalent :c=c+a
-=:c-=a equivalent :c=c-a
= :c=a equivalent :c=c*a
/= : c/=a equivalent :c=c/a

Compare the warm husband

==
!=

<

=
<=

Logical operators

and
or
not

member operator

in: Returns if a value is found in the specified sequence true, Otherwise return to false
not in: Returns... If no value is found in the specified sequence true, Otherwise return to false

Operator priority

** The highest
~: Bit reversal

python Common sentences

if

if-else

if-elif

if condition1:
statement_block1
elif condition2:
stationment_block2

if nesting

Loop statement

while loop

while condition:
station_block
condition by false, sign out
It's true , perform statement_block Then calculate again condition Value

Let's judge the expression first , Executing statement

for

for iterating_var in sequence
statement_block

break sentence

Terminated

continue

Terminate the loop , Execute next cycle

break、continue In nested statements , It only works for the nearest layer of circulation

pass sentence

pass It's airspace data , Not doing anything , It's like a placeholder

What is a string

Escape character

String output and input

String output

Expression statement ,print() function
Using the write() Method , Standard output files can be used sys.about quote
format :str.format() Function to format the output value
If the output value needs to be converted into a string , have access to repr() perhaps str() Function to implement .
str(): Function returns a user-friendly expression
repr(): Produce an interpreter readable form of expression
s = ‘Hello’
str(s)

repr(s)
"'Hello

String input

python Provides a input() Built in functions , It can read a line of text from standard input

Access the value in the string

How to store strings

Strings belong to immutable sequences
Short string resident mechanism , For short strings , When assigning it to multiple different objects , There is only one copy in memory , Multiple objects share the copy . However , This does not apply to long strings , That is, long strings do not conform to the resident mechanism . When a long string is assigned to multiple variables , These variables do not share the same memory address .

Use slice to intercept string

You can use square brackets to intercept strings

From the first to the last number

String infidelity function

find

find() and rfind() Methods are used to find the position of the first and last occurrence of a string in the specified range of another string , Returns if it does not exist -1

index

index() and rindex() Method is used to return the position of the first and last occurrence of one string in the specified range of another string , If not, throw an exception

count

Returns the number of times a string appears in the current string

replace

Search and replace

splite

split() and rsplit() Methods are used to specify characters as separators , Split the current string from left to right or from right to left into multiple strings , And return the list containing the segmentation results .

split() and rsplit() Method also allows you to specify the maximum number of divisions

about split() and rsplit() Method , If you do not specify a separator , Any white space in the string will be considered a delimiter , And the continuous blank symbols are regarded as a separator
However , Explicitly pass parameter assignments split() When using the separator of , The situation is different .

capitalize

capitalize() Converts the first character of a string to uppercase

title

Convert the first letter of each word to uppercase

startswith

Determines whether the string starts with the specified string

endswith

Determines whether the string ends with the specified string

upper

Convert a string to an uppercase string

String alignment function

enter(),ljust(),rjust(), Returns a new string of the specified width , The original string is centered , Left aligned or right aligned format appears in the new string , If the specified width is greater than the string length , Use the specified characters to supplement
zfill() Returns a string of the specified width , To the left, use the character 0 Fill in ,

To string function strip

Remove the specified characters at the beginning or end of the string , Then return a result , But the contents of the original string will not change

Addition operation

‘hello '+‘world’
hello world

“a” in “abcde”

Multiplication

‘abcd’*3


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