Python_day_01_python基礎

1.python 2.7 是python2.x後的最後一個版本,

更新到3.x但企業多用2.x

    python2.x   python3.x
1.  print "hello"   print ("hello")
2.  5/2 = 2     5/2=2.5
    5/2.0=2.5
3.  input()     input()
    raw_input() 
from __future__ import print_function   ##導入3.X的打印模塊

python編寫的豆瓣,知乎,Google ,SALTSTACK 等

2.python優缺點

1) python優點

簡單、優雅、明確(最主要)
有強大的第三方庫模塊
可跨平臺移植
一種面向對象的語言

2) python缺點

•代碼執行速度慢,相比C語言,不過現在python的
異步併發框架導致執行速度慢;
•python是開源的編程語言,代碼不能加密;
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
但爬蟲多用Python,Java。因爲傳輸受網速瓶頸限制,代碼執行速度幾乎忽略不計
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

3.python安裝

•訪問python官網:www.python.org;
•Linux操作系統上一般iso鏡像裏面自帶,直接通過yum安裝;

4.建立並執行Python腳本

vim hello.py
sh hello.py
或者
chmod +x /mnt/hello.py
./hello.py
或者
python hello.py

5.python腳本

•#!/usr/bin/python 這種寫法表示直接引用系統的默認的
Python 版本;
•#!/usr/bin/env python 這種寫法表示,引用環境變量裏面
自定義的 Python 版本, 具有較強的可移植性;
中文編碼需要指定編碼格式

6.指定編碼格式的方法

• #coding:utf-8
• #coding=utf-8
• #encoding:utf-8
• #encoding=utf-8

7.Python插件

1) ipython ##強化的python,可以自動補齊

2) Pycharm ##python圖形化軟件

tar xf pycharm-community-2017.1.4.tar.gz -C /opt/
cd pycharm-community-2017.1.4/bin/
./pycharm.sh

8.輸入與輸出

要求:輸入某學生的三門課程成績,計算出該學
生的平均成績。
提示:(course1+course2+course3)/3

#!/usr/bin/env python
#coding:utf-8
    from __future__ import division     ##保留小數
    Chinese = input("score1:")
    Math = input("score2:")
    English=input("score3:")

    avg = (Chinese+Math+English)/3
    print "平均成績爲:%.2f" %(avg)

這裏寫圖片描述

9.數據類型

""" ##塊代碼註釋 也可以做行模式規範
#   <快捷鍵:ctrl + />  ##行代碼註釋
%f  ##小數,浮點數
%.2f    ##保留兩位小數點的浮點數
%d  ##整形數
%s  ##字符串
%o  ##八進制
%x  ##十六進制
"%d" %1 ##輸出1
"%3d" %1    ##%nd,當n>數長時,決定對齊與填充
"%.3d" %1   ##%.nd,保留小數點後n位

print [‘130%.3d’%(i) for i in range(1,30)] ##生成130001,130002…130029

mem_percent = 30
“%.2f%%” %(mem_percent) ##輸出結果爲30.00%,兩個%%強制轉譯,顯示%


anInt = 12
print type(anInt)       ##顯示數據類型

aLong = 12l         ##l和L都表長整型
bLong = 12L
print type(aLong)
print type(bLong) 
print type(anInt + aLong)   ##整形與長整型相加,結果位長整型

aFloat = 12.34
print type(aFloat)  ##類型爲float
bFloat = 1.2e10     ##e和E都表10的n次方
cFloat = 1.2E10
print type(bFloat)
print type(cFloat)

aComplex = 2+3j     ##複數位complex型
print aComplex.real
print aComplex.imag
print aComplex.conjugate()
print type(aComplex)

1) 數值類型是可變數據類型麼?

aInt = 1
bInt = 1
print id(aInt),id(bInt)     ##測試結果id相同

cInt = 100
print "before cInt:%s" %(id(cInt))
cInt = 101
print "after cInt:%s" %(id(cInt))       ##測試結果id不同

In [29]: a = 1
In [30]: type(a)
Out[30]: int

In [31]: long(a)    ##強轉換
Out[31]: 1L

In [32]: complex(a)
Out[32]: (1+0j)

2) 如何刪除數字對象

del cInt
print cInt

3) 布爾型:True(1). False(0)

例子

判斷閏年(4年一閏,百年不潤,400又閏)

a = input("please input a year :")
b = (a % 4 == 0) and (a % 100 != 0) or (a % 400 == 0)
print b

這裏寫圖片描述

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ctrl+alt+l ##快捷自動分割補齊
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

year = input(‘Year:’)
exp = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
if exp:
print “%s 是閏年” %(year)
else:
print “%s 不是閏年” %(year)

用戶登錄界面

file >> settings >> File and Code Templates >> Python Script
設置默認格式

#!/usr/bin/env python
#coding:utf-8

"""
Name: ${NAME}.py
Author: Devin
Date: ${DATE}
Connection:[email protected]
Desc:

"""

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
a=1
b=2
print a if a > b else b ##三目運算符

在標註處 alt + Enter ##自動添加導入包

import getpass
getpass.getpass() ##輸入密碼不顯示,但只能用terminal執行

import time
starttime=timt.time()

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

if條件語句

Python的if語句:注意縮進

    if 表達式:
        if-suite

    if 表達式:
        if-suite
    else:
        else-suite

循環語句

while 表達式:
循環執行的語句

while 表達式:
循環執行的語句
else:
不符合循環條件執行的語句

while True: ##死循環

while True:
    cmd = raw_input(">>>")

    if cmd == "":
        continue
        # print "cmd"
    elif cmd == "q":
        break
    else:
        print cmd

內置函數

abs() ##函數返回x(數字)的絕對值。
這裏寫圖片描述
coerce(1, 1.0) ##數據類型轉換函數,返回一個包含類型轉換完畢的兩個數值元素的元組
這裏寫圖片描述
divmod(5, 2) ##內建函數把除數和餘數運算結果結合起來,返回一個包含商和餘數的元組。
這裏寫圖片描述
pow(2,3) ##進行指數運算
這裏寫圖片描述
round(2.456657,2) ##用於對浮點數進行四捨五入運算
這裏寫圖片描述

例子

##1.ATM登陸系統
#!/usr/bin/env python
# coding:utf-8

import getpass

user = "root"
passwd = "redhat"

username = raw_input("用戶名:")
password = getpass.getpass("密碼:")
if username == user and password == passwd:
    print "%s用戶登陸成功!" % (username)
    print  """
                ATM管理系統

        1. 取款
        2. 存款
        3. 查詢餘額
        4. 退出
   """
    choice = input("請輸入你的選擇:")
    if choice == 1:
        pass
    elif choice == 2:
        pass
    elif choice == 3:
        pass
    elif choice == 4:
        exit(0)
    else:
        print "請輸入正確的選擇!"
else:
    print "%s 用戶登陸失敗!" % (username)

2.ATM登錄系統——while

#!/usr/bin/env python
# coding:utf-8

import getpass

# 數據庫中存儲的用戶名和密碼;
user = "root"
passwd = "redhat"

# 已登陸的次數;
trycount = 0

# 登陸次數小於3,允許再次登陸;
while trycount < 3:
    print "%s次登陸........" %(trycount+1)
    username = raw_input("用戶名:")
    password = getpass.getpass("密碼:")
    if username == user and password == passwd:
        print "%s用戶登陸成功!" % (username)
        print  """
                    ATM管理系統

            1. 取款
            2. 存款
            3. 查詢餘額
            4. 退出
       """
        while 1:
            choice = input("請輸入你的選擇:")
            if choice == 1:
                print "取款"
            elif choice == 2:
                print "存款"
            elif choice == 3:
                print "查詢餘額"
            elif choice == 4:
                exit(0)
            else:
                print "請輸入正確的選擇!"
    else:
        print "%s 用戶登陸失敗!" % (username)

    trycount += 1

# 登陸次數超過三次,報錯;
else:
    print "登陸次數超過3次, waiting......"

3.求偶數和

#!/usr/bin/env python
# coding:utf-8

import time

start_time = time.time()
num = 2
sort = 0
while num <= 10000:
    sort += num
    num += 2
print sort
end_time = time.time()

print  "run %s" % (end_time - start_time)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章