python初學day1

shell與python比較:
shell腳本:

#!/bin/bash
echo hello
sh shell.sh

python腳本:

#!/usr/bin/python   ##或者#!/bin/python或者#!/usr/bin/env python
print "hello"
python hello.py

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

python的解釋器

cpython     ##默認 交互式
ipython     ##cpython的增強
jpython
pypy

1. I/O輸入輸出

input("")
output()
***)
In [2]: input("請輸入數字")
請輸入數字1
Out[2]: 1
***)

將數值保存在裏面
In [4]: a = input("please input number:")
please input number:2

In [5]: print a ##或者直接輸入a
2       ##結果爲2


input() 和raw_input()的區別:##raw_input可以輸出字符input只能輸數字
In [6]: a = raw_input("number:")
number:f

In [7]: a
Out[7]: 'f'

2.輸入三個學生成績並計算平均值

#!/usr/bin/env python
#encoding=utf-8
from __future__ import division
a=input("input  score C:")
b=input("input score B:")
c=input("input score A:")
avg= (a+b+c)/3
print avg

在版本2中除法只保留整數部分,如果需要計算出小數可以用以下兩個方法: 
【1】from __future__ import division
【2】5/2.0  除數寫成浮點型

數值類型:

%f  小數,浮點數
%.2f    保留兩位小數點的浮點數
%d  整形數
%s  字符串
%o  八進制
%x  十六進制

變量類型:

輸入:
anInt = 2
print type(anInt)
aLong = 12L
print type(aLong)
print type(anInt+aLong)
aFloat = 2.0000
print type(aFloat)
bFloat = 1.2E10
cFloat = 0.12e11
print bFloat
print cFloat
aComplex = 2+3j
print aComplex.real
print aComplex.imag
print type(aComplex)
運行結果:
<type 'int'>
<type 'long'>
<type 'long'>
<type 'float'>
12000000000.0
12000000000.0
2.0
3.0
<type 'complex'>

數值類型是不可變的
a=1
b=1
print id(a),id(b)
c=1
d=2
print id(c),id(d)

pyhton的內存分配機制,數值相同時會指向同一個地址
當改變數值的時候,將會重新分配一個內存地址,不會在原有內存地址中修改數值 

刪除數字對象
del c   ##加上變量
print c

布爾型變量:True(1) False(0)

3.判斷是否爲閏年

a = input("請輸入年份:")
b = (a % 100 != 0 and a % 4 == 0 )or (a % 400 ==0)
if b:
    #pass是佔位關鍵字
    #pass
    print "%s 是閏年" %(a)
else:
    print "%s 不是閏年"%(a)

注:ctrl+alt+L自動調整已經寫好的語句

4.判斷輸入用戶名密碼是否正確

username = raw_input("username:")
passwd = raw_input("password:")
if (username == "root") and (passwd == "redhat"):
    #pass
    print "ok"
else:
    print "not correct!"

5.登陸成功後進入系統界面(補充密碼加密但不能在pycharm裏面需要在terminal中)

import getpass
username = raw_input("username:")
passwd = getpass.getpass("password:")
if (username == "root") and (passwd == "redhat"):
    #pass
    print "ok"
    print """
                學生管理系統
        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 "not correct!"


6.登陸三次自動退出

trycount = 0
while trycount < 3:
    print "登陸…………"
    trycount += 1
else:
    print "登陸次數超過3次,請稍後再試!!"

7.整合 添加循環

trycount = 0
while trycount < 3:
    print "登陸…………"
    import getpass
    username = raw_input("username:")
    passwd = getpass.getpass("password:")
    if (username == "root") and (passwd == "redhat"):
        # pass
        print "ok"
        print """
                    學生管理系統
            1.查詢課表
            2.查詢成績
            3.選課系統
            4.退出
        """
        while True:
            choice = input("請輸入你的選擇:")
            if choice == 1:
                # pass
                print """
                    數學:99
                    語文:100
                    英語:120
                    """
            elif choice == 2:
                pass
            elif choice == 3:
                pass
            elif choice == 4:
                exit(0)
            else:
                print "請輸入正確的選擇!"
        else:
            print "not correct!"
    trycount += 1
else:
    print "登陸次數超過3次,請稍後再試!!"

break:遇到關鍵字跳出當前的循環,不再繼續執行循環
continue:跳出本次循環,不執行本次循環中continue後面的語句 

8.體會break和continue再循環中的不同

while True:
    cmd = raw_input(">>>")
    if cmd == "":
        continue
        print cmd
    elif cmd == "q":
        break
    else:
        print cmd

9.求1-1000內所有偶數和

count=2
sum=0
while count <= 1000:
    sum += count
    count += 2
print sum

10.for循環
求1-1000內所有偶數和
alt+回車 快速導入函數
方法一:(這裏我們導入了時間函數,方便對比)用時0.17024

import time
start_time = time.time()
sum = 0
for i in range(2,2000000,2):
    sum += i
print sum
end_time = time.time()
print "run %s" %(end_time - start_time)

方法二:用時0.0406010150909

import time
start_time = time.time()
print sum(range(2,2000000,2))
end_time = time.time()
print "run %s" %(end_time - start_time)

11.階乘

while True:
    a = input("請輸入一個數字:")
    x=1
    for i in range(1, a + 1):
            x = x*i
    print x

 

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