python 笔记(一)
1、Python优点
简单,优雅,明确
强大的模块第三方库
易移植
面向对角
可扩展
2、缺点
代码不能加密
执行速度慢
3、变量定义
第一个字母必须是字母表中的大小写,或下划线。不能以数字为开头。
1)变量赋值举例
eg:
>>> x=123
>>> y=x
>>> id(x)
22582176
>>> id(y)
22582176
>>> x=100
>>> id(x)
22580736
>>> id(y)
22582176
>>> tast-date = 1
File "<stdin>", line 1
SyntaxError: can't assign to operator ; 不能以非大小写下划线定义变量
>>> tast_date = 1
2)变量值类型
布尔型:true,false
eg:
If True: print ‘ddd’
ddd
整型,长整型,浮点型:
eg:
>>> type(a)
<type 'long'>
>>> a = 2**34
>>> type(a)
<type 'int'>
>>> a=1.34
>>> type(a)
<type 'float'>
>>> a=3
>>> type(a)
<type 'int'>
>>> b=2.3
>>> type(b)
<type 'float'>
字符串:
>>> name='wang'
>>> type(name)
<type 'str'>
序列类型:列表,数组……
>>> name_list=['wang','bai','gui']
>>> type(name_list)
<type 'list'>
4、运算
a) "/" 除法,默认取只为整,可跟小数点。
>>> 3/2
1
>>> 3.0/2
1.5
b) 取被除数
>>> 10//2
5
>>> 10//4
2
>>> 10//3
3
c) 加减乘除法
+= “c+=a等于c=c+a”
*= “c*=a等于c=c*a”
**= “c**=a等于c=c**a”
d) 与运算&:
10和15的与运算
1010 1111 è10
10 20
1010 10100 è
>>> 10 & 20
0
>>> 10 & 15
10
e) 或运算:
10 20
1010 10100 è 11110 30
>>> 10 | 20
30
5、注释
单行注释:#
多行注释:三个章引号’’’ ‘’’或者三个双引号””” “”” ,另一种也可做为格式化输出
提示:单引号和双引号无区别
6、理解字符编码
三种字符级:ASSIC(默认一个字节) Unicode(两个字节) UTF-8(可变字节,汉字三个字节)
概述:一个字节8位 111111 256个字,两个字节16位 65536个字
1024字节=1KB
1)(ACCIS)
一个字节举例:
>>> ord('a')
97 ; a对应8位的某几位
>>> ord('A')
65 ;A对应8位的某几位
相当于每个对应256里个的各一位,一一对应。
2)UTF-8编码:可变的
存英文用一个字节存,汉字用三个字节存。
>>> a='wang'
>>> type(a)
<type 'str'>
>>> a=u'wang'
>>> type(a)
<type 'unicode'>
>>> a
u'wang'
>>> name=u'王小哥'
>>> type (name)
<type 'unicode'>
>>> name
u'\u738b\u67cf\u8d35'
>>> name = "王小哥"
>>> name
'\xe7\x8e\x8b\xe6\x9f\x8f\xe8\xb4\xb5'
>>> name = u"王小哥"
>>> name
u'\u738b\u67cf\u8d35'
3)unicode转换成UTF-8
>>> name = u'王小哥'
>>> name
u'\u738b\u67cf\u8d35'
>>> name.encode('utf-8')
'\xe7\x8e\x8b\xe6\x9f\x8f\xe8\xb4\xb5'
4)UTF-8转换成unicode
>>> wang="王小哥"
>>> wang
'\xe7\x8e\x8b\xe6\x9f\x8f\xe8\xb4\xb5'
>>> wang.decode('utf-8')
u'\u738b\u67cf\u8d35'
提示:
python系统里默认是ASSIC码编码格式,对应一个字节,所以在python里面存中文,会有问题,应该转换成UTF8编码格式
#_*_ coding:utf-8 _*_
Name = u”中文”
Print name
提示:
系统中读到内存里默认是unicode格式,存到硬盘可以以UTF-8格式存,因为unicode默认都是以两个字节存的,占空间。
8、导入模块
三种导入方式:
1) import modulename
2) from module import sayHi
3) import moduleName as newname
eg:
Import sys
Print sys.argv
或
From sys import argv
Print argv
或
From sys import * ;不建议使用
或
Import multiprocessing as nulte
=========================
eg:
a) 调用系统命令
>>> import os
>>> os.system('df')
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18423556 1691736 15795936 10% /
tmpfs 405824 0 405824 0% /dev/shm
/dev/sda1 198337 29668 158429 16% /boot
0 ;默认会输出返回上一条指令的返回值
==> 将返回值存入并输出
>>> cru_dir = os.system('pwd')
/root/python/day01
>>> print cru_dir
0
b) 如何将输出结果输出?
倒入import commands模块
>>> import commands
>>> res = commands.getstatusoutput('pwd')
>>> res
(0, '/root/python/day01')
3) 倒入import sys模块
[root@localhost day01]# cat test1.py
import sys
print sys.argv
print sys.argv[2]
[root@localhost day01]# python test1.py a b c
['test1.py', 'a', 'b', 'c']
b
9、用户交互
1) raw_input
[root@localhost day01]# cat test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input('please input your name:')
age = raw_input('age:')
print name , age
[root@localhost day01]# cat test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input('please input your name:')
age = raw_input('age:') ;raw_input无论输入任何都当字符串来解释
job = raw_input('job:') ;可通过int(raw_input(‘age:’)) 转换成数字,或直接用input(‘age:’),注意:imput后面跟的是原生态,之前是什么,就是什么,定要指明是什么类型等,不然会有错误。
salary = raw_input('salary:')
print '''
name: %s
age : %s
job : %s ;%s代表字符串,%d代表数字,%f代表浮点数
salary : %s
-----------------
''' %(name,age,job,salary)
Imput举例:
[root@localhost day01]# vim test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input('please input your name:')
age = input('age:')
job = raw_input('job:')
salary = raw_input('salary:')
print type(age)
print '''
name: %s
age : %s
job : %s
salary : %s
-----------------
''' %(name,age,job,salary)
[root@localhost day01]# python test2.py
please input your name:wangbaigui
age:28
job:it
salary:2w
<type 'int'>
name: wangbaigui
age : 28
job : it
salary : 2w
-----------------
[root@localhost day01]# vim test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
AGE = 28
name = raw_input('please input your name:')
age = input('age:')
job = raw_input('job:')
salary = raw_input('salary:')
print type(age)
print '''
name: %s
age : %s
job : %s
salary : %s
-----------------
''' %(name,age,job,salary)
[root@localhost day01]# python test2.py
please input your name:wangbaigui
age:AGE
job:it
salary:3w
<type 'int'>
name: wangbaigui
age : 28
job : it
salary : 3w
10、流程控制
1) if… else…举例:
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input('please input your name:')
age = input('age:')
job = raw_input('job:')
salary = raw_input('salary:')
if age > 30:
meg = 'you are so old...'
elif age >20:
meg = ‘…’
else:
meg = 'you are so yongest...'
print '''
name: %s
age : %d
job : %s
salary : %s
-----------------
%s
''' % (name,age,job,salary,meg)
2) for循环
[root@localhost day01]# cat test4.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input('please input your name:')
job = raw_input('job:')
salary = raw_input('salary:')
relea_age = 28
for i in range(10):
age = input('age:')
if age >30:
meg = "think small..."
elif age == 28:
meg = "good!,you are right."
break
else:
meg = "go to think"
print meg
print "you have only %s times to trye" %(9 - i)
print '''
name: %s
age : %d
job : %s
salary : %s
-----------------
%s
''' % (name,age,job,salary,meg)
3)while循环
[root@localhost day01]# cat test5.py
slect_num = input('which num you want:')
count = 0
while count < 100:
if count == slect_num:
print 'you slect right:%s'%(slect_num)
choice = raw_input('you want go on or contine (Y/N)')
if choice == 'Y':
while True:
slect_num = input('which num you want agan:')
if slect_num <= count:
print "lookup alred past..,ples input newest num!"
else:
break
continue
else:
break
else:
print 'lookup',count
count +=1
else:
print 'Alread more then 100.. so Bye!'
python 笔记(一)的更多相关文章
- Python笔记之不可不练
如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- 20.Python笔记之SqlAlchemy使用
Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...
- Python笔记——类定义
Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- 13.python笔记之pyyaml模块
Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...
- 8.python笔记之面向对象基础
title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
- python笔记 - day7-1 之面向对象编程
python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...
- python笔记 - day7
python笔记 - day7 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 面向对象,初级篇: http://www.cnblog ...
- python笔记 - day6
python笔记 - day6 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 大纲: 利用递归,实现阶乘: Python反射 pyt ...
随机推荐
- 家庭局域网接入Internet
接入Internet 建立宽带连接 步骤一:单击"网络",再属性 步骤二:单击"更改适配器设置" 步骤三:选择"宽带连接" 步骤四:输入 ...
- Android实例-如何使用系统剪切板(XE8+小米2)
结果: 发现个问题,就是粘贴时会清除之前的信息. unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, S ...
- SQL2008-表对表直接复制数据
1.全部复制,使用简单,但是字段容易出错(字段和顺序必须相同) INSERT INTO AAAStuffAgitationYield SELECT * FROM StuffAgitationYiel ...
- iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...
- OSStartHighRdy()一去不复返【worldsing笔记】
有关于uCos的启动过程讲解有很多文章,这里主要记录已下OSStartHighRdy()在stm32下是怎么写和运行的: stm32上电 运行SystemInit(): 配置时钟,这个不要也可 ...
- java properties 文件中书写相对路径
工程src下的properties 文件要引用发布到D:\work\apache-tomcat-7.0.52\webapps\项目名称\certs这个地址下的文件,properties 中的文件路径应 ...
- matlab color_rain colorbar
来自http://www.aos.wisc.edu/~dvimont/matlab/Graphics_Tools/color_rain.html Listing of script color_rai ...
- Scribefire发CSDN博客
历史 在非常久非常久曾经,CSDN是支持外部工具来写文章的,但是在还有一个非常久非常久曾经就不行了. 突然看到CSDN有能够用外部工具来写博客了(CSDN的公告),一直以来都纠结这个问题,CSDN的编 ...
- Flash 无法输入中文的修正方法
在某些运行模式或运行时环境中,Flash 有一个 Bug,文本框与键盘的交互模式会无法输入中文(包括日文等带有输入法状态栏的输入模式),只要对 TextField 文本框实例的 FocusEvent. ...
- 关于自定义adapter使用getApplicationContext()影响主题
最近弄了一个东西,listview+switch构成界面 关于android自定义adapter,继承自baseadapter,发现界面的switch开关主题变了想要的是浅色的主题,但是却发现变成了深 ...