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

Python environment construction and input and output

編輯:Python

PythonEnvironment construction and input and output

一、Python概述

1、計算機資源

在開發領域,Computer resources can be divided into two parts:軟件資源 + 硬件資源

軟件資源:看得見,摸不著

硬件資源:看得見,摸得著

硬件資源(CPU、內存、硬盤、風扇、電源、鍵盤、鼠標…)

軟件資源(Office辦公軟件、網易雲音樂、various computer games)

思考:我們發現,software and hardware can actually interact,這是什麼原理呢?

答:使用操作系統,操作系統It is the bridge between computer software and hardware

2、操作系統分類

在日常的應用中,Operating systems can be roughly divided into three categories:

① Windows操作系統

② MacOS操作系統

③ Linux操作系統(The most used operating system on the server side)

3、為什麼要學習Python

① 技術趨勢

PythonComes with star attributes,The popularity ranks among the top three in the programming language industry

② 簡單易學

less development code,Accurately express requirement logic;33個關鍵字,7種基本數據類型;語法規則簡單,close to natural language.

③ 應用廣泛

Python語言涉及IT行業70%above technical fields

4、Python語言的缺點

① PythonIts operating speed is relative toC/C++/Javaslightly slower

② Python由於語言的特性,Unable to encrypt code

③ Python的版本之間,Compatibility is not ideal(Python2和Python3)

5、Python語言介紹

Python是一種跨平台computer programming language⾔. is a high-level combination了解釋性、編譯性、互動性和面向對象的腳本語⾔.最初被設計用於編寫自動化腳本Shell(適用於Linux操作系統),with the version不Update and add new functions of language,逐漸被用於獨立的、大型項目的開發.

Actually many famous machine learning、⼈⼯Intelligence and deep learning frameworks are also based onPython語⾔進⾏開發的:

Google開源機器學習框架:TensorFlow

開源社區主推學習框架:Scikit-learn

百度開源深度學習框架:Paddle

6、Python2.x和Python3.x版本的區別

在目前的Python領域,There are two main application versions:Python2和Python3

The main difference can be understood as:輸入、Differences in output and encoding formats

Python2.x

Python3.x

​ Python3.6、Python3.7、Python3.8、Python3.9…

在生產環境中,我們⼀Generally do not choose the latest versionPython,because there may be unknownBug,所以⼀It is strongly recommended that you choose the software version when,向前推1 ~ 2個版本.So our course mainly explainsPython3.7版本.

二、Python解析器

1、Python解析器的作用

demo.py

print('Hello World')

由於Python屬於高級語言,It doesn't directly in the computer operation,因為缺少Python語言的運行環境:Python解析器

Python解析器的作用:就是把PythonThe code is converted into machine language that the underlying computer can recognize,如0101…

2、Python解析器的種類

① CPython,C語言開發的解釋器[官方],應⽤Extensive interpreter.

② IPython,基於CPython的一種交互式解釋器.

③ 其他解釋器

PyPy,基於Python語言開發的解釋器.

JPython,運⾏在Java平台的解釋器,直接把Python代碼編譯成Java字節碼執⾏.

IronPython,運⾏在微軟.Net平台上的Python解釋器,可直接把Python代碼編譯成.Net的字節碼.

3、下載Python解析器

下載地址:https://www.python.org/downloads/release/python-379/

[單擊上述鏈接] – 查找目標文件:Windows x86-64 executable installer – 單擊即可下載.

4、Python解析器的安裝

第一步:雙擊運行Python的解析器,選擇自定義安裝以及添加Python到環境變量

第二步:Select all the function of the menu to install,默認全部勾選

pip:Python的包管理工具,Can be used to install the future of our project need to use the various modules

第三步:設置PythonParser installation path,It is strongly recommended to install in addition toC盤以外的盤符

第四步:測試PythonIs the parser usable

按Windows + R,輸入cmd字符,打開Windows的DOS窗口,輸入python(全部小寫),如下圖所示:

出現了以上界面,就代表Python3.7The parser has been installed successfully.How to exit from this window toDOS模式呢?

答:使用exit()方法

>>> exit() 回車

三、Python開發工具PyCharm

1、為什麼要安裝PyCharm

工欲善其事必先利其器

在Python的開發領域,Its development tools are very, very many,EditPlus、Notepad++、Sublime Text3、Visual Studio Code、PyCharm(The most powerfulIDE)

2、PyCharm的主要作用

PyCharm是⼀種Python IDE (集成開發環境),帶有一整套可以幫助用戶在使用Pythonimprove the efficiency of language development⼯具,內部集成的功能如下:

Project管理

智能提示

語法高亮

代碼跳轉

調試代碼

解釋代碼(解釋器)

框架和庫

3、PyCharm的分類

PyCharm一共有兩個版本:專業版(收費) 與 社區版(免費、開源)

In the base class,PyCharmCommunity edition is enough for us,綽綽有余.

4、下載PyCharm

下載地址:https://www.jetbrains.com/pycharm/download/#section=windows

5、PyCharm安裝

第一步:雙擊PyCharm軟件安裝包,進行軟件安裝

第二步:設置軟件的安裝路徑,theoretically no requirement,However, it is recommended to putC盤以外的盤符

第三步:PyCharm基本設置,Create desktop icons with.py文件關聯

6、PyCharm軟件的使用

* 創建Python項目

什麼是項目?其實我們在實際開發中,Every time you participate in the development of a job is the development process of a project.所以使用PyCharmThe first thing is to learnPython項目的創建過程.

第一步:創建項目

第二步:設置項目路徑,必須放在C盤以外的盤符(非常重要!!!)

配置完成後,單機Create創建Python項目.

* New file and code writing

If the file to be uploaded to the server in the future,Then the file name must not be in Chinese.

編寫Hello World

print('Hello World')

* 運行代碼

運行結果:

* set or replacePython解析器

打開File文件,找到Settings設置,如下圖所示:更換Python解析器

* PyCharmsoftware settings

① 軟件主題(The future of software)

② Encoding font settings

③ Code font size settings(文字大小)

打開File文件 => Settings設置,find interface settings:

主題設置:

Set the font and font size:

字體設置:

字號設置:

* Open and close projects

打開項目:The project itself already exists,我們直接打開.

選擇項目目錄(文件夾)即可,如下圖所示:

① This Window => overwrite current item⽬,從⽽打開目標項目

② New Window => In a new window⼝打開,則打開兩次PyCharm,每個PyCharmresponsible for an item⽬

③ Attach => merge two projects together,in the same window

關閉項目:Close the running project.

四、Python注釋

1、注釋的作用

Emphasize one thing first:Python代碼 => Python解析器 => 機器語言,But the annotation goes throughPythonThe interpreter does not parse and execute.Because it is mainly for code comments.

注釋作用:提高代碼的閱讀性

在我們編寫Python程序時,為了了提高程序的可讀性,It is strongly recommended that you add comments to the core code.

2、PythonBasic syntax of comments

* 單行注釋

單行注釋,以"#"(Shift + 3)號開頭,只能注釋一行內容

# 注釋內容

示例代碼:

第一種:above the line of code

# 輸出Hello World字符串
print('Hello World')

第二種:behind the code(code behind2個空格)

print('Hello World') # 輸出Hello World字符串

* 多行注釋

多行注釋:Can comment multiple lines of code or programs at the same time,Used in the annotation of code blocks

基本語法:

""" 注釋內容 第一行 第二行 第三行 """

'''
注釋內容
第一行
第二行
第三行
'''

示例代碼:

""" Hi, 大家好 I'm a friend of a dark horse programmer 從今天開始,我們將一起學習Python這門語言 """
''' Hi, 大家好 I'm a friend of a dark horse programmer 從今天開始,我們將一起學習Python這門語言 '''
print('Hi, 大家好')
print('I'm a friend of a dark horse programmer')
print('從今天開始,我們將一起學習Python這門語言')

* PyCharm注釋小技巧(快捷鍵)

在PyCharm中,我們可以使用Ctrl + /斜槓to quickly comment on code or programs.

五、PyCharm常用快捷鍵

1、代碼提示

在PyCharm中,當我們輸入Pythonbefore the keyword2~3個字符,It will automatically prompt the code.這個時候,We just need to press Enter to quickly enter something.

2、代碼保存

編寫代碼時,Be sure to get into a good habit,使用Ctrl + SSave code quickly.

個人建議,When writing a line of code,Just as once.

3、撤銷與恢復

If accidentally deleted one line of code,At this time, we can quickly pressCtrl + Zquick recovery.每按一次就撤銷一次,If you cancel more,怎麼辦?

答:還可以通過Ctrl + Y進行恢復操作

六、Python中的變量(重點)

1、Variable Learning Objectives(案例)

案例:實現兩個變量的交換

1號杯:可樂

2號杯:牛奶

經過一系列Python操作以後

1號杯:牛奶

2號杯:可樂

2、引入變量的概念

什麼是量:A quantity is the smallest unit in a program.

那什麼是變量呢?

① 變量是存儲數據的容器

② Variables are quantities that can be changed while the program is running

③ The data stored in the variable is temporary

3、變量的作用(舉個栗子)

Taobao registration case:

① 寫入用戶名、密碼

② PythonProgram to receive username and password(臨時存儲)

③ Permanently store the username and password you just received(數據庫)

為了解決以上問題,Pythondeveloped the concept of variables,Some information entered by the user,temporarily save,The container that is stored isPython變量.

4、變量的定義

基本語法:

變量名稱 = 變量的值
注:Leave a space on both sides of the equal sign,其實PythonTry to keep a space on both sides of the suggested symbol in

說明:在Python程序中,This equal sign is not the same as the equal sign in everyday life,It has a professional term:賦值運算符,its reading:To be read from right to left,Put the value of a variable = 賦值給左邊的變量.

5、變量的命令規則

標識符命名規則是PythonA naming convention when defining variable names in,具體如下:

① 由數字、字母、下劃線(_)組成

② 不Can start with numbers

③ 嚴格區分⼤小寫

④ 不能使⽤Built-in keywords as variable names

下劃線 => Shift + -減號

舉個栗子:

① abc、abc123、_abc、hello(合理)

② 123abc、@abc、abc-123(不合理)

③ _(下劃線) => Can this be a variable name??答:可以

for _ in range(10):
...

④ 變量abc和變量ABCYao is a variable?答:不一樣,These are two completely different variables

⑤ 記不住33what to do with a keyword?答:借助於help()方法

>>> help('keywords')

6、Recommend variable naming rules

① Variable naming must be known by the name.

② 大駝峰:即每個單詞首字母都大寫,例如: MyName .

③ 小駝峰:第二個(含)以後的單詞首字母大寫,例例如: myName .

④ 下劃線:例如: my_name .

7、Variable definition and call

在Python中,記住:變量一定要先定義,後使用,否則會報錯.

定義:

name = 'itheima'
address = '北京市順義區京順路99號'

調用:

print(name)
print(address)
或
print(name, address)

8、Definition and use of variables frequently asked questions

① How do variables differ from strings:

在Python中,If the content to be assigned is added with single or double quotes,其就是Python中的一種數據類型:叫做字符串(Text messages in everyday life)

② print打印變量時,like to add quotes to it

print(name) # 輸出變量name對應的值
與
print('name') # 輸出'name'這個字符串

七、Python中變量的數據類型

1、為什麼要學習數據類型

The definition of variables is very simple,But many friends may think:Variables in addition to storing the data of character types,Can other types of data be stored??其實,在 Python中,我們為了應對不the same business needs,Will be divided into the data不同的類型,如下圖所示:

面試題:請手寫出Python中的7種數據類型?

答:數值類型、布爾類型、字符串類型、列表類型、元組類型、集合類型、字典類型

Today we only need to understand the former3種即可.

問題:How to determine what type a variable is?

答:① 使用type(變量名稱)方法,返回變量的數據類型 ② isinstance(變量名稱,數據類型),只能返回True或False(真的還是假的)

2、數值類型

數值類型就是我們日常生活中的數字,Digital is divided into two forms:整數 與 小數(帶小數點)

整數類型:int類型

小數類型:float類型

案例1:定義一個人的信息,姓名:Tom、年齡18歲

name = 'Tom'
age = 18
print(type(age))

案例2:Define a supermarket checkout system,Write a name:大白菜,價格:3.5

name = '大白菜'
price = 3.5
print(type(price))

3、布爾類型

布爾類型是與邏輯相關一種數據類型,只有兩個值:True(真)與False(假)

案例1:手工定義一個flag變量,其值為True

flag = True
print(flag)
print(type(flag))

其實在Python中,The return result of many programs can also beTrue或False,比如isinstance()

num = 10
print(isinstance(num, int)) # True
print(isinstance(num, bool)) # False

4、字符串類型

在Python變量定義中,如果其賦值的內容是通過單引號或雙引號引起來的內容就是字符串str類型.

msg = '這家伙很懶,什麼都沒有留下...'
print(type(msg))

5、其他類型(了解)

# 1、list列表類型
list1 = [10, 20, 30, 40]
print(type(list1))
# 2、tuple元組類型
tuple1 = (10, 20, 30, 40)
print(type(tuple1))
# 3、set集合類型:去重
set1 = {
10, 20, 30}
print(type(set1))
# 4、dict字典類型:查詢、搜索
dict1 = {
'name':'itheima', 'age':18}
print(type(dict1))

八、了解Python中的Bug

1、認識一下bug

所謂bug,就是程序中的錯誤.如果程序有錯誤,We need our programmers to troubleshoot,及時糾正錯誤.

2、解決bug三步走

第一步:View the error page

第二步:see wrong line number

第三步:According to the specific error,具體分析

3、PyCharm代碼調試(重點)

Debug工具是PyCharm IDEIn the integrated tools specially used to debug the program,Here the programmer can check program execution details and process,so that we can quickly find the program'sBug!

Debug工具使⽤二步走:① 打斷點 ② Debug調試

4、下斷點

Breakpoint should be put in which position:答:The first line of the code snippet where the code might go wrong

5、Debug調試

6、單步調試

When you encounter a small lightning icon, it represents this line,something may have gone wrong.

九、Python中的格式化輸出

1、格式化輸出

目前為止,All of our output is passed directly throughprint(變量名稱)In the form of print directly.但是實際工作中,We may need to format the output of the variable(output in a certain format).

2、百分號格式化輸出

基本語法:

...
print(變量名稱)
print('字符串%格式' % (變量名稱))
print('字符串%格式 %格式 %格式' % (變量名稱1, 變量名稱2, 變量名稱3))

%The common format is as follows:

格式符號轉換%s字符串%d有符號的十進制整數%f浮點數%c字符%u無符號十進制整數%o八進制整數%x十六進制整數(小寫ox)%X十六進制整數(大寫OX)%e科學計數法(小寫’e’)%E科學計數法(大寫’E’)%g%f和%e的簡寫%G%f和%E的簡寫

案例:定義兩個變量name=‘itheima’, age=18,Output in the following format:我的名字是itheima,今年18歲了.

案例:定義兩個變量title=‘大白菜’,price=3.5,Output in the following format:Vegetable specials today,Chinese cabbage only3.5元/斤.

title = '大白菜'
price = 3.5
# 格式化輸出“Vegetable specials today,Chinese cabbage only3.5元/斤."
print("Vegetable specials today,%s只要%.2f元/斤." % (title, price))

其實除了%fYou can set the number of decimal places other than,%dYou can also fill in the serial number.

案例:定義兩個變量id=1,name=‘itheima’,Output in the following format:姓名itheima,學號000001

id = 1
name = 'itheima'
print("姓名%s,學號%06d" % (name, id))

3、format方法格式化輸出

基本語法:

...
print('字符串{}'.format(變量名稱1))
print('{}字符串{}'.format(變量名稱1, 變量名稱2))

案例:定義兩個變量,name=‘孫悟空’,mobile=‘18878569090’,Output in the following format"姓名:孫悟空,聯系方式:18878569090"

name = '孫悟空'
mobile = '18878569090'
print("姓名:{},聯系方式:{}".format(name, mobile))

4、formatMethod shorthand for formatting output(推薦)

在Python3.6以後版本,為了簡化format輸出操作,introduced a shorthand:

name = '孫悟空'
mobile = '18878569090'
print(f'姓名:{
name},聯系方式:{
mobile}')

5、Escape symbols in formatted output

在字符串中,如果出現了\t和\n,It represents the meaning of two escape characters

\t :制表符,一個tab鍵(4個空格)的距離
\n :換行符

案例:

print('*\t*\t*')
print('hello\nworld')

特別說明:默認情況下,每個print()方法執行完畢後,都會輸出一個\n換行符.如果不想讓print()方法換行,可以添加一個end參數

print('*', end='')

十、Python中的標准輸入

1、Why do you need to enter

到目前為止,All our programs can only output data to the user.但是實際工作中,We often enter to get the user's input information,Such as the password input in the banking system、Taobao in the user login validation.

2、input()輸入方法

在Python中,如果想讓PythonThe program accepts user input,可以使用input()方法

基本語法:

input()

But often onlyinput()方法,其意義不大,We should also use a variable to temporarily accept user input,It is convenient for later operation.

變量名稱 = input('提示信息:')

案例:In the banking system,輸入密碼的過程

password = input('請輸入您的銀行卡密碼:')
print(f'The bank card password you entered is:{
password}')

3、input()Method Important

記住:所有由input()The data obtained by the method is==“字符串”==類型

name = input('請輸入您的姓名:')
age = input('請輸入您的年齡:')
print(type(name)) # <class 'str'>
print(type(age)) # <class 'str'>
print('*\t*\t*')
print('hello\nworld')

特別說明:默認情況下,每個print()方法執行完畢後,都會輸出一個\n換行符.如果不想讓print()方法換行,可以添加一個end參數

print('*', end='')

十、Python中的標准輸入

1、Why do you need to enter

到目前為止,All our programs can only output data to the user.但是實際工作中,We often enter to get the user's input information,Such as the password input in the banking system、Taobao in the user login validation.

[外鏈圖片轉存中…(img-lcvORuvD-1659170183515)]

2、input()輸入方法

在Python中,如果想讓PythonThe program accepts user input,可以使用input()方法

基本語法:

input()

But often onlyinput()方法,其意義不大,We should also use a variable to temporarily accept user input,It is convenient for later operation.

變量名稱 = input('提示信息:')

案例:In the banking system,輸入密碼的過程

password = input('請輸入您的銀行卡密碼:')
print(f'The bank card password you entered is:{
password}')

3、input()Method Important

記住:所有由input()The data obtained by the method is==“字符串”==類型

name = input('請輸入您的姓名:')
age = input('請輸入您的年齡:')
print(type(name)) # <class 'str'>
print(type(age)) # <class 'str'>

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