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

Fundamentals of python (I)

編輯:Python

List of articles

  • Computer composition
  • Python Basics
    • Python effect
    • Interpreter and Pycharm
    • notes
    • Variable
      • identifier
    • Debug
    • data type
    • Operator
  • Program control statement
    • Output
    • Input
    • if sentence
    • loop
  • character string

Computer composition


CPU: Processing instructions and operational data
Memory : Temporary storage CPU Data to process
Hard disk : Permanently store data
common sense : The network card is often restarted . Because the network card is because CPU Constantly calling data from the hard disk , Causes memory to become full . Restart will clear the memory .

Python Basics

Python effect


Google Open source machine learning framework :TensorFlow
Open source community promotes learning framework :Scikit-learn
Baidu open source deep learning framework :Paddle

Interpreter and Pycharm

Python Interpreter function : Run the file

(1) species :
CPython:C The interpreter of language development ( official ), A widely used interpreter .
IPython: be based on CPython An interactive interpreter .
PyPy: be based on Python language ⾔ Developed interpreter .
Jython: shipment ⾏ stay Java Platform interpreter , Put... Directly Python Code compiled into Java Bytecode execution ⾏.
IronPython: shipment ⾏ At Microsoft .Net On the platform Python Interpreter , You can directly Python Code compiled into .Net Bytecode .

(2)Python Environment Download

(3)Pycharm Related knowledge
PyCharm yes ⼀ Kind of Python IDE( Integrated development environment ), with ⼀ The whole set can help ⽤ Hu is making ⽤Python language ⾔ Development time withdrawal ⾼ Its efficiency is ⼯ have , The functions of internal integration are as follows :Project management + Smart tips + grammar ⾼ bright + Code jump + Debugging code + Explain the code + Frames and Libraries

(4)Pycharm Basic use of
term ⽬ root ⽬ Record or root ⽬ Record any position inside — Right click -- [New] – [Python File]– transport ⼊⽂ Piece name -- [OK]. If it is to be uploaded to the server in the future ⽂ Pieces of , that ⽂ Keep in mind that the item name cannot ⽤ in ⽂.

Modify interpreter :

notes

Annotations fall into two categories : single ⾏ Comments and more ⾏ notes .(ctrl+/)
single ⾏ notes : Only comment ⼀⾏ Content ,

# Statement content 
print("hello Python") # Output ( Simple instructions can be put into ⼀⾏ After the code ⾯,⼀ After getting used to the code ⾯ Add two spaces and write a comment ⽂ word )

many ⾏ notes : You can comment more ⾏ Content ,⼀ like ⽤ In the comments ⼀ The case of this code .

""" The first ⼀⾏ notes The first ⼆⾏ notes Third ⾏ notes """
''' notes 1 notes 2 notes 3 '''

Variable

In the program , Data is temporarily stored in Memory in , To find or make faster ⽤ This data , Usually we define this data after it is stored in memory ⼀ Names , The name is the variable .

Defining variables : Variable name = value

identifier

By digital 、 word ⺟、 Underline composition
Cannot start with a number
Can't make ⽤ Built in keywords
Strictly distinguish between ⼤⼩ Write

Debug

bug An error in a program . We can use the break point and Debug Try to check bug.
Debugger: Show variables and variable details
Console: Output content

data type

How to detect data types :type()

a = 1
print(type(a))# <class 'int'> -- integer 
b = 1.1
print(type(b))# <class 'float'> -- floating-point 
c = True
print(type(c))# <class 'bool'> -- Boolean type 
d = '12345'
print(type(d))# <class 'str'> -- character string 
e = [10, 20, 30]
print(type(e))# <class 'list'> -- list 
f = (10, 20, 30)
print(type(f))# <class 'tuple'> -- Tuples 
h = {
10, 20, 30}
print(type(h))
# <class 'set'> -- aggregate 
g = {
'name':'TOM', 'age':20}
print(type(g))# <class 'dict'> -- Dictionaries 

Functions that convert data types :

# 1. float() -- Convert to floating point 
num1 = 1
print(float(num1))
print(type(float(num1)))
# 2. str() -- Converts to string type 
num2 = 10
print(type(str(num2)))
# 3. tuple() -- take ⼀ Convert a sequence into a tuple 
list1 = [10, 20, 30]
print(tuple(list1))
print(type(tuple(list1)))
# 4. list() -- take ⼀ Convert a sequence into a list 
t1 = (100, 200, 300)
print(list(t1))
print(type(list(t1)))
# 5. eval() -- Convert the data in the string to Python Expression primitive type 
str1 = '10'
str2 = '[1, 2, 3]'
str3 = '(1000, 2000, 3000)'
print(type(eval(str1)))
print(type(eval(str2)))
print(type(eval(str3)))

Operator

Arithmetic operator :
Assignment operator

Comparison operator

Comparison operator
Logical operations between numbers

a = 0
b = 1
c = 2
# and Operator , As long as there is ⼀ The values are 0, The result is 0, Otherwise, the result will be the last ⼀ individual ⾮0 Numbers 
print(a and b)# 0
print(b and a) # 0
print(a and c) # 0
print(c and a)# 0
print(b and c)# 2
print(c and b) # 1
# or Operator , Only all values are 0 The result is 0, Otherwise, the result is ⼀ A non 0 Numbers 
print(a or b)# 1
print(a or c)# 2
print(b or c)# 1

Program control statement

Output

Format output :

# My name is TOM
print(' My name is %s'%name)
# My student number is 0001
print(' My student number is %4d'%student_id)
# My weight is 75.50 Male ⽄
print(' My weight is %.2f Male ⽄'%weight)
# My name is TOM, This year, 18 Year old 
print(' My name is %s, This year, %d Year old '% (name, age))
print(' My name is %s, This year, %s Year old '% (name, age))
#%s It can also be used to output integers , floating-point 
# My name is TOM, Next year, 19 Year old 
print(' My name is %s, Next year, %d Year old '% (name, age+1))
# My name is TOM, Next year, 19 Year old 
print(f' My name is {
name}, Next year, {
age + 1} Year old ')
# f'{ expression } yes python3.6 The format method added later 

Escape character
\n: in ⾏.
\t: tabs ,⼀ individual tab key (4 A space ) Distance of .

print(' Output content ',end="\n")
# Two print Line feed output 

stay Python in ,print(), Default ⾃ belt end="\n” This change ⾏ Terminator , So it leads to every two print Directly change ⾏ Exhibition ,⽤ You can change the terminator as needed .

Input

Program reception ⽤ Households lose ⼊ The function of data is to input ⼊.

input(" Prompt information ")

When the program executes ⾏ To input when , wait for ⽤ Households lose ⼊, transport ⼊ Do not continue down until you are finished ⾏.
stay Python in ,input receive ⽤ Households lose ⼊ after ,⼀ Generally stored in variables ,⽅ So that ⽤.
stay Python in ,input Will send any received ⽤ Households lose ⼊ All the data are treated as character string Handle .

password=input(' Please input a password :')
print(f' The password you entered is {
password}');

if sentence

if true:
print(" Execute statement 1");
print(" Execute statement 2");
# Next ⽅ Your code is not indented to if Sentence block , So and if Conditions ⽆ Turn off 
print(' I am a ⽆ On whether the conditions are ⽴ All have to hold ⾏ Code for ')

demand :⽤ Users can output ⾃⼰ In the age of , Then the system goes into ⾏ Judge whether you are an adult or not , Adults export " Your age is ’⽤ Households lose ⼊ In the age of ’, Has reached adulthood , Yes ⽹".

age = int(input(' Please lose ⼊ Your age :'))
if age>= 18:
print(f' Your age is {
age}, Has reached adulthood , Yes ⽹')
print(' System off ')
if Conditions :
On condition that ⽴ Of board ⾏ Code for 1
On condition that ⽴ Of board ⾏ Code for 2
else:
The conditions don't work ⽴ Of board ⾏ Code for 1
The conditions don't work ⽴ Of board ⾏ Code for 2

Multiple judgments

if Conditions 1:
Conditions 1 become ⽴ Of board ⾏ Code for 1
Conditions 1 become ⽴ Of board ⾏ Code for 2
elif Conditions 2:
Conditions 2 become ⽴ Of board ⾏ Code for 1
Conditions 2 become ⽴ Of board ⾏ Code for 2
else:
None of the above conditions ⽴ Of board ⾏ Of board ⾏ Code for

demand :
1. If you have money , You can go to ⻋
2. On ⻋ after , If you have a seat available , You can sit down ⻋ after , If there are no empty seats , Then stand and wait for an empty seat. If you have no money , You can't go to ⻋

money = 1
seat = 0
if money == 1:
print('⼟ Howe , Not bad money , Go smoothly ⻋')
if seat == 1:
print(' There are seats available , You can sit down ')
else:
print(' There are no empty seats , Station etc. ')
else:
print(' Don't have the money , You can't go to ⻋, Chasing the bus ⻋ run ')


Ternary operator :
On condition that ⽴ Of board ⾏ The expression of if Conditions else The conditions don't work ⽴ Of board ⾏ The expression of
demand : Stone scissors and paper games

import random
computer=random.randint(0,2)
print(computer)
player=int(input(' What you input is 0 stone 1 scissors 2 cloth '))
if ((player == 0) and (computer == 1)) or ((player == 1) and (computer == 2) )or((player == 2) and (computer == 0)):
print(' Players win ')
elif player == computer:
print(' It ends in a draw ')
else:
print(' The computer wins ')

loop

while Conditions :
On condition that ⽴ Repeated execution ⾏ Code for 1
On condition that ⽴ Repeated execution ⾏ Code for 2

demand 1: Calculation 1-100 Summation of

i=1
result=0
while i<=100:
result+=i
i+=1
print(result)

demand 2: Calculation 1-100 Sum of even integers

i=0
result=0
while i<100:
i+=2
result+=i
print(result)

demand 3: Print asterisk ( Upper triangle )

j=0
while j<=4:
i=0
while i<=j
print('*')
i+=1
print()
j+=1

demand 4: multiplication table

i=1
while i<=9:
j=1
while j<=i:
print(f'{
i}*{
j}={
i*j}',end='\t')
j+=1
print()
i+=1
while Conditions :
On condition that ⽴ Repeated execution ⾏ Code for
else:
After the cycle ends normally, execute ⾏ Code for
i = 1
while i<= 5:
if i == 3:
print(' It's not true this time ')
break
print(' daughter-in-law ⼉, I was wrong ')
i += 1
else:
print(' My daughter-in-law has forgiven me , Really open ⼼, Ha ha ha ha ')

So-called else It refers to the execution of... After the normal end of the cycle ⾏ Code for , That is, if it is break end ⽌ The cycle ,else Next ⽅ Indented code will not execute ⾏.

i = 1
while i<= 5:
if i == 3:
print(' It's not true this time ')
continue
print(' daughter-in-law ⼉, I was wrong ')
i += 1
else:
print(' My daughter-in-law has forgiven me , Really open ⼼, Ha ha ha ha ')

because continu Yes, exit the current ⼀ Secondary cycle , Go ahead ⼀ Secondary cycle , So the cycle is continue It can end normally under control , When the cycle is over , Then hold ⾏else
Indented code .

for Temporary variable in Sequence :
Repeated execution ⾏ Code for 1
Repeated execution ⾏ Code for 2
for Temporary variable in Sequence :
Repeated execution ⾏ Code for
else:
After the cycle ends normally, execute ⾏ Code for

character string

  1. features :
name1 = 'Tom'
name2 = "Rose"
name3='''i am 1'''
# String in three quotation marks ⽀ Hold change ⾏.
# Create string I'm Tom?
c = "I'm Tom"
d = 'I\'m Tom'
  1. Input and output
name = 'Tom'
print(' My name is %s'%name)
print(f' My name is {
name}')
password = input(' Please lose ⼊ Your password :')
print(f' You lose ⼊ The password for is {
password}')
  1. section
    cut ⽚ It refers to intercepting the operation object ⼀ Part of the operation .
 Sequence [ Starting position subscript : End position subscript : Step ⻓]
1. It does not contain the data corresponding to the ending position subscript ,
Both positive and negative integers are OK ;( Left closed right away )
2. Step ⻓ Is the selection interval ,
Both positive and negative integers are OK , Default step ⻓ by 1
name = "abcdefg"
print(name[2:5:1])# cde
print(name[2:5])# cde
print(name[:5])# abcde
print(name[1:])# bcdefg
print(name[:])# abcdefg
print(name[::2])# aceg
print(name[:-1])# abcdef, negative 1 Means the last ⼀ Data 
print(name[-4:-1])# def(-1 Represents the last data , And so on )
print(name[::-1])# gfedcba The steps are negative , Indicates the selection in reverse order 
print(name[-4:-1:-1])# Data cannot be selected ,-1 To -4 From left to right , but -1 Step size indicates that... Is selected from right to left 
  1. lookup
    String search ⽅ The method is to find ⼦ The position or number of occurrences of a string in a string .
    (1)find(): Detect a ⼦ Whether the string is included in this string , If you return this ⼦ The subscript at the beginning of the string , Otherwise return to -1.
 String sequence .find(⼦ strand , Starting position subscript , End position subscript )
mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and'))
# 12
print(mystr.find('and', 15, 30))
# 23
print(mystr.find('ands'))
# -1

(2)index(): Detect a ⼦ Whether the string is included in this string , If you return this ⼦ The subscript at the beginning of the string , Otherwise, an exception will be reported
(3)rfind(): and find() Function the same , But find ⽅ Start on the right .
rindex(): and index() Function the same , But find ⽅ Start on the right .
count(): Go back to some ⼦ The number of times the string appears in the string

  1. modify
    Modifying strings , It refers to modifying the data in a string in the form of a function .replace() There is a return value , Is the modified string , But the original string will not be modified . The original string has not been modified , What is modified is the return value . Description string is an immutable data type .
    If the split character is a substring of the original string , The substring will be lost after splitting .

  2. Judge
    The so-called judgment is to judge whether it is true or false , The result is a Boolean data type :True or False.




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