stay python in : Variable , It's the name of the object !!
Variable names are not random , There are certain rules .
Variable names can be made up of letters 、 Numbers and underscores , for example var
、var_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
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)
(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)
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.
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])
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 ~
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
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 .
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 .
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 .
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
Python for Loop can traverse any sequence of items , Like a list or a string .
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
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 .
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