python入門學習

一. python工作模式

交互式:直接打開python使用,退出後不能保存

[root@foundation77 ~]# python3.6 
Python 3.6.6 (default, Jan 12 2019, 08:09:33) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

2.文本模式:可以保存和修改

二.python 註釋方法

單行註釋: #

多行註釋: “”“    ”“”  三個雙引號之間的內容可以被註釋

三. python 語言的特點

  1. 沒有分號結尾的特點
  2. python 語言需要嚴格的縮進。
  3. 保存爲 .py 結尾的文件,python 就可以直接運行

四. 數據類型

整形 int ,浮點型 float,字符串 str .

>>> name = 'linux'               #定義字符用冒號標記
>>> print (type(name))
<class 'str'>
>>> age = 10
>>> print(type(age))
<class 'int'>
>>> number = 3.14
>>> print(type(number))
<class 'float'>
>>> 

格式化輸出

%s    字符串
%d    整形
%f    浮點型

>>> name = 'westos'
>>> age = 10
>>> print('%s年齡爲:%d' %(name,age))
westos年齡爲:10

>>> age = '10'                               # 定義爲字符串
>>> print('%s年齡爲:%d' %(name,age))         # 格式錯誤,不能爲%d(整形)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> print('%s年齡爲:%d' %(name,int(age)))     #轉爲整形,格式正確
westos年齡爲:10


>>> money = 6666.6
>>> print('工資爲:%f' %(money))
工資爲:6666.600000                    #浮點型默認保留6位
>>> print('工資爲:%.1f' %(money))
工資爲:6666.6                         
>>> print('工資爲:%.2f' %(money))
工資爲:6666.60
>>> 


>>> scale = 0.5
>>> print('數據比例是 %.2f%%' %(scale * 100))   #輸出百分號要加兩個 %%
數據比例是 50.00%

布爾數據類型:不爲空

>>> a = 'westos'
>>> bool(a)
True
>>> a = '.'
>>> bool(a)
True
>>> a = ''
>>> bool(a)
False
>>> a = '[][]==='
>>> bool(a)
True
>>> a = ' '
>>> bool(a)
True

五. 變量

 變量是代表或引用某值的名字,

 用10 代表age : >>> age = 10

 將10 賦給變量age ,age 即爲變量名。 變量名可以包括下劃線,字母,數字,且不能以數字開頭。

刪除變量

>>> a = 1.2
>>> a
1.2
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined


更改輸出位置

>>> a = 'start'
>>> a.center(50)
'                      start                       '
>>> a.center(50,'*')
'**********************start***********************'
>>> print(a.center(50,'^'))
^^^^^^^^^^^^^^^^^^^^^^start^^^^^^^^^^^^^^^^^^^^^^^
>>> 

六. 簡單的python程序

"""
- 輸入學生姓名
- 依次輸入學生的三門科目成績
- 計算該學生的平均成績,並打印
- 平均成績保留一位小數
- 計算語文成績佔總成績的百分比,並打印
"""

name = input("學生姓名:")
Chinese = float(input("語文成績:"))
Math = float(input("數學成績:"))
English = float(input("英語成績:"))

# 總成績
SumScore = Chinese + Math + English
# 平均成績
AvgScore = SumScore / 3

ChinesePercent = (Chinese / SumScore) * 100

print('%s 的平均成績爲%.1f' % (name, AvgScore))
print('語文成績佔總成績的%.2f%%' % ChinesePercent)



/home/kiosk/PycharmProjects/python/westos/bin/python /home/kiosk/.PyCharmCE2018.2/config/scratches/scratch.py
學生姓名:ming
語文成績:82
數學成績:92
英語成績:72
ming 的平均成績爲82.0
語文成績佔總成績的33.33%

Process finished with exit code 0

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章