python基础1
一、python2和3的区别:
1.字符编码
python2.7中
#_*_coding:utf-8_*_ print '你好'
python3.5中不需要为讨厌的字符编码而烦恼
print('你好')
2.print
python3.5中需要加()
3.某些库改名
old name new name
_winreg winreg
ConfigParser configparser
copy_reg copyreg
Queue queue
SocketServer socketserver
markupbase _markupbase
repr reprlib
test.test_support test.support
区别还有很多,后期慢慢发现补充...
二、python安装
cd Python-3.5.0
./configure --prefix=/usr/local --enable-shared
make
make install
ln –s /usr/local/bin/python3 /usr/bin/python3(软链接)
windows
1
2
3
4
5
6
7
|
1 、下载安装包 https: / / www.python.org / downloads / 2 、安装 默认安装路径:C:\python27 3 、配置环境变量 【右键计算机】 - - 》【属性】 - - 》【高级系统设置】 - - 》【高级】 - - 》【环境变量】 - - 》【在第二个内容框中找到 变量名为Path 的一行,双击】 - - > 【Python安装目录追加到变值值中,用 ; 分割】 如:原来的值;C:\python27,切记前面有分号 |
三、hello world程序
在linux 下创建一个文件叫hello.py,并输入
#!/usr/bin/env python
print("hello world")
如此一来,执行: ./hello.py
即可。
ps:执行前需给予 hello.py 执行权限,chmod 755 hello.py
注释:
当行注视:# 被注释内容
多行注释:""" 被注释内容 """
四、用户输入
#!/usr/bin/env python
#_*_coding:utf-8_*_
#name = raw_input("What is your name?") #only on python 2.x
name
=
input
(
"What is your name?"
)#3.5
print
(
"Hello "
+
name )
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
getpass
# 将用户输入的内容赋值给 name 变量
pwd
=
getpass.getpass(
"请输入密码:"
)
# 打印输入的内容
print
(pwd)
五、模块
(1)sys
import
sys
print
(sys.argv)
$ python test.py helo world
[
'test.py'
,
'helo'
,
'world'
]
#把执行脚本时传递的参数获取到了
(2)os
import os
os.system(
"df -h"
)
#调用系统命令
(3)tab补全模块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #!/usr/bin/env python 2 # python startup file 3 import sys 4 import readline 5 import rlcompleter 6 import atexit 7 import os 8 # tab completion 9 readline.parse_and_bind('tab: complete') 10 # history file 11 histfile = os.path.join(os.environ['HOME'], '.pythonhistory') 12 try: 13 readline.read_history_file(histfile) 14 except IOError: 15 pass 16 atexit.register(readline.write_history_file, histfile) 17 del os, histfile, readline, rlcompleter
六、表达式if ... else和while循环
一、在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了。如果猜错超过三次,询问是否继续。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
age = 21 counter = 0 for i in range(10): if counter < 3: guess = int(input("please input your guess age:")) if guess == age: print("how lucky you are:"),guess elif guess > age: print("please try it smaller:") else: print("please try it larger:") else: print("Do you want to try it again?") num = input("please choose:") if num == 'y': counter = 0 continue else: break counter += 1
*代码中如果用户输入前面的变量用input的话会出错。input = input("please choose:")这是不行的
二、模拟用户名密码登录,用户名可以无限输入,密码输入三次失败退出。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = 'hongpeng' passwd = 123 counter = 0 while True: Name = input("please input your name:").strip() if Name == name: for i in range(3): Passwd = int(input("please input your passwd:")) if Passwd == passwd: print('----------you login in-----------') break else: print('failed,please try it again...') break else: print('please input right name') flag = False
三、海枯石烂死循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
count = 0 while True: count += 1 if count > 50 and count < 60: continue#不执行下面 print("你是风儿我是沙,缠缠绵绵到天涯。。。",count) if count == 100: print("去你妈的风和沙,我要的是money",count) break#跳出整个循环
七、数据类型
2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子,其中-5,4为实数,j为虚数,数学中表示复数是什么?。
int(整型)
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
"hello world"
name
=
"hongpeng"
print
"i am %s "
%
name
#输出: i am hongpeng
PS: 字符串是 %s;整数 %d;浮点数%f