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

Python Basics

編輯:Python

python

1. Variables and comments

stay python in : Variable , It's the name of the object !!

1.1 Naming rules for variables

Variable names are not random , There are certain rules .

  • Variable names can be made up of letters 、 Numbers and underscores , for example varvar_var1_vaqw12 wait .

  • Variable names can start with letters or underscores , such as var1, perhaps _var.

  • But variable names cannot start with numbers , image 4var This is the wrong way to write .

  • Variable names cannot contain spaces .

  • Variable names cannot be associated with keyword The same name ; The variable name should not be the same as Built in functions The same name

    Be careful : stay Python Medium case sensitive

2. character string

2.1 Definition of string

String can be used Double quotes 、 Single three quotation marks 、 Double triple quotes Definition

Three quotation marks can There are multiple lines of direct writing Substring of , for example :

hello = """ Hello , The world : Hello! ! Your test was successful !! Okay """
print(hello)

2.2 There are quotation marks in the string

(1) There is a single quotation mark in the string content , So when we define this string , You should use double quotation marks . for example :

hello = "He said: 'ok, I will go now!'"
print(hello)

(2) There are double quotation marks inside the string , So when we define this string , You should use single quotation marks . for example :

hello = 'He said: "ok, I will go now!"'
print(hello)

(3) There are double quotation marks inside the string content , When there are single quotation marks , You should use three quotation marks . for example :

hello = """"He said: 'ok," I will go now!'"""
print(hello)

2. 3 String splicing

Splicing is a common operation of strings , You can use the plus sign + Connect two strings . Generate a new string . for example :

str1 = "this is "
str2 = "a test. "
str3 = str1 + str2
print(str3)

Output results :

this is a test.

2.4 String element index

A string is composed of elements **【 character 】**. for example : Hello , The world ~ This is from 6 Made up of... Characters .

hello = " Hello , The world ~"

The above string , The primary index of each character is :

you The index of is 0, When printing :print(hello[0])

good The index of is 1, When printing :print(hello[1])

, The index of is 2, When printing :print(hello[2])

the The index of is 3, When printing :print(hello[3])

world The index of is 4, When printing :print(hello[4])

~ The index of is 5, When printing :print(hello[5])

python It also supports the use of A negative number indicates the index of the string , The last character index is -1, The penultimate index is -2 By analogy .

you The index of is -6, When printing :print(hello[-6])

good The index of is -5, When printing :print(hello[-5])

, The index of is -4, When printing :print(hello[-4])

the The index of is -3, When printing :print(hello[-3])

world The index of is -2, When printing :print(hello[-2])

~ The index of is -1, When printing :print(hello[-1])

2.5 String slice

Sliced consciousness ; In short , We just want to read some part of the string . for example :

hello = " Ran Junze , Hello , The world is very big ~"
print(hello)
print(hello[4: 12]) # Only read from 4 Character start , To 12 The substring between characters 

Output results :

 Ran Junze , Hello , The world is very big ~
Hello , The world is very big ~

2.5.1 Omit the index of a slice

If we want to get ’ Hello, Mr. Liu ’ among How do you do this 3 A word How to slice it ?

It can be used hello[2:5]

We found that , What you want to cut out goes all the way to the end of the string , You can also use hello[2:] , Later index Don't write when it's empty , Represents the end of the entire string .

Corresponding , If The previous index Don't write , Can be said Slice from the beginning of the string , such as hello[:2] , Is to cut out Liu Zong These two words

2.6 Get string length

Built in functions len Can be used to get the length of the string , It also gets the number of characters in the string

such as

var = ' President trump '
lenth = len(var)
print(lenth)

The return is 5, because var The length of the string corresponding to the variable is 5 Characters .

3. function

A general understanding of computer language , function : Means to complete a function ( Mission ), A collection of statement blocks specifically composed of .

Python in , This is how we define functions :

def add(a, b): # Define a function that adds two numbers 
c = a + b
return c
a = 10
b = 5
print(add(a, b))

Output results :15

def Is the key word Means to define a function

Then there is the function name , Then there is a need for Brackets , In the middle of this bracket is Parameters

And finally a The colon , The indented code below indicates what the function name represents , be called The body of the function .

The code of the function body needs to be indented . We recommend indenting four spaces .

To execute the code in the function body , must Call function .

interview() Call Call function interview , Sometimes it's called Execute function interview.

When the interpreter Execute to This line of code calls the function , I'll run to function interview Inside , To execute the statements in the function .

  • Function must be defined first , You can call .
Call and return

Be careful , When the interpreter finishes executing the internal code of the called function , Will return To the code that calls it , Continue with the rest of the code .

In the example above , After execution add After the corresponding function internal code , Will return to the calling function , Carry out the following sentence .

** Built in functions :**python Language developers know stay python The interpreter has already implemented some functions for us , Call it a built-in function .

3. 1 Function parameter

4 Input information

Input and output python There are corresponding built-in functions ; Output :print, Input :input.

input The parameter in the function is a string , That is to say input Read from the console as a string .

If you want to read in numbers to get other types of data , stay input Function is read in and then converted ; for example :

salart = input(" Please enter salary :")
intSalary = int(salart) # Force to int Type data 
realSalary = intSalary * 75 / 100
print(realSalary)

Output results :

 Please enter salary :3000
2250.0

Loop statement

for Loop statement

Python for Loop can traverse any sequence of items , Like a list or a string .

Recycling else sentence

Sample code :

for num in range(10, 20):
for i in range(2, num):
if num % i == 0:
j = num / i
print('%d be equal to %d * %d' % (num, i, j))
break
else:
print('%d It's a prime number ' % num)

Output results

10 be equal to 2 * 5
11 It's a prime number
12 be equal to 2 * 6
13 It's a prime number
14 be equal to 2 * 7
15 be equal to 3 * 5
16 be equal to 2 * 8
17 It's a prime number
18 be equal to 2 * 9
19 It's a prime number

while loop

Python Programming while Statements are used to loop programs , Under certain conditions , Loop through a program , To deal with the same tasks that need to be repeated . Its basic form is :

while Judge the condition (condition):
Execute statement (statements)……

Sample code :

count = 0
while (count < 9):
print('this count is : %d' % count)
count += 1
print('end!')

Output results :

this count is : 0
this count is : 1
this count is : 2
this count is : 3
this count is : 4
this count is : 5
this count is : 6
this count is : 7
this count is : 8
end!

** Infinite loop ( Dead cycle ):** If the conditional statement is always true, The cycle will go on indefinitely .

Use in loop else

Sample code :

count = 0
while count < 5:
print('%d is less than 5' % count)
count += 1
else:
print('%d not less than 5' % count)

Output results :

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 not less than 5

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