您的位置:首页 > 编程语言 > Python开发

Python_day_01_python基础

2018-03-16 13:58 393 查看

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:zerotoerodevin@163.com
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python