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

猜你喜欢

转载自blog.csdn.net/qq_41636653/article/details/82592654