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

python基础

2015-11-05 00:00 681 查看
1.python字节码

#!/usr/bin/env python
# encoding: utf-8
#字节码
#被调用的模块会自动生成pyc文件,这个文件就是字节码
#导入顺序,默认直接执行pyc文件,如果py与pyc文件内容不一致就先导入py
import m
print "hello"

2.字符

C语言中没有字符串概念,有字符概念

C用字符数组伪造的字符串,用连续一块

字符串特性,一旦修改,重新创建

"hello"+"sb"+"yys"

"hello"

"hello"+"sb"

"hello"+"sb"+"yys"

会开辟出三块内存区域用于存放

python会自动释放前两块,垃圾回收机

值类型:改变会重新开辟

引用类型:会跟着改变

1.加载内存

2.词法分析

3.语法分析

4.编译

5.字节码

6.机器码

编码:ascii:256个 unicode、utf-8

#引用类型:会跟着改变

#1.加载内存

#2.词法分析

#3.语法分析

#4.编译

#5.字节码

#6.机器码

#编码:ascii:256个 unicode、utf-8

3.运算符

#!/usr/bin/env python
# encoding: utf-8

#+ - * /
#%取余
#**幂
#//取整
print 1+1
print 3-1
print 5/2
print 5.0/2
print 5%2
print 5**2
print 10//3

# == 比较、=赋值、 >大于、 <小于、  >=大于等于、 <=小于等于
#i +=1----i=i+1

#逻辑
#and、 or 、not
#成员运算
#in、not in

4.变量声明

#name变量名  yangyinshen为变量值  name----》指向yangyinshen存储的内存地址
name = 'eddy'

#yangyinshen这个内存位置并没有消失,只是重新开出一块存放yys name---》指向yys内存地址
name = 'eddy'

name1='eddy'
name2=name1
#name不变
name1='123'

#变量名第一个不能是数字,不能以内置名做为变量名

5.基本数据类型

#!/usr/bin/env python
# encoding: utf-8

#单值 数字(整型123、长整形1....1、浮点型3.14、复数)、字符串、布尔值(true false 1 0)
#集合 列表、元组、字典、hash表
#整型
#32位系统 整数位数为32位
#64位系统 整数位数为64位

#字符串
#格式化输出
print 'name is %s' %('yys')
print 'name is %s,age is %d' %('yys',23)

name = 'name is {0},age is {1}'
print name.format("yys",20)

#索引
a = 'alex'
print a[0]#y

print a[0:1]#第一个
print a[0:2]#第一个到第二个,不是到第三个
print a[0:]#第一个到最后一个
print a[1:]#第二个到最后一个
print a[-1]#最后一个
print a[:-1]#第一个到倒数第二个

#a字符的数目
print len(a)
print a[len(a)-1],a[-1]

#去除空格
name = '      alex    ok     '
print name.strip()
#name.lstrip()
#name.rstrip()

#list
name_list = ['alex','yys','linux']
#name_list = list(['alex','yys','linux'])
print name_list[0]
print name_list[-1]
names = 'alex,yys'
#得到一个列表
names.split(',')
#列表追加,内存地址不会变
name_list.append('seven')
#删除列表中的最后一个
del name_list[-1]

#join
name_list = ['alex','yys','linux']
print '_'.join(name_list)

print "alex" in name_list

#tuple不可变更
ages = (11,12,13,14,56,23)

ages = [11,12,13,14,56,23]
for i in ages:
if i == 12:
print i
break#break跳出循环进入下一代码块,continue本次循环结束进入下一次循环
else:
 
3ff0
;       pass

while True:
print 'ok'

#字典 键值对,保存方式无序
dict = {
'name':'alex',
'age':18,
'gender':'male'
}
#索引
print dict['name']
#只打印key
for i in dict:
print i
#打印键值对
for i,j in dict.items():
print i,j

#打印所有key、所有values、所有key-values
dict.keys()
dict.values()
dict.items()

6.输入输出

#!/usr/bin/env python
# encoding: utf-8

name = raw_input('please input name:')
#让输入不可见
import getpass
pwd = getpass.getpass("please input password")

7.流程控制

#!/usr/bin/env python
# encoding: utf-8
#a=123
#if a == 123:#值对比,不是内存位置对比
#    print "ok"
#else:
#    print "error"
#name = raw_input('please input values:')
#if name == 'a':
#    print 'a'
#elif name == 'b':
#    print 'b'
#elif name == 'c':
#    print 'c'
#else:
#    print 'low b'
import getpass
name = raw_input("please input name:")
pwd = getpass.getpass("please input password:")

#if name == 'a' and pwd == '123':
#    print 'login success'
#    print 'normal user'
#elif name == 'b' and pwd == '123':
#    print 'login success'
#    print 'normal admin'
#elif name == 'c' and pwd == '123':
#    print 'login success'
#    print 'normal super admin'
#else:
#    print 'login failed'
if pwd == '123':
if name == 'a'
print 'login success'
print 'normal user'
elif name == 'b':
print 'login success'
print 'normal admin'
elif name ==  'c':
print 'login success'
print 'normal super amdin'
else:
print 'login failed'
else:
print 'login failed'

8.操作文件

#!/usr/bin/env python
# encoding: utf-8

#找到文件
#./1.txt

#打开文件
#file open
#file
# w 写,存在就覆盖,不存在就创建
# r 只读
# a 追加,文件存在,追加内容,不存在就创建
# r+ 读写
# w+ 写读 坑爹
#最好读和写分开操作
file_obj = file('1.txt','r')

#读写文件
#read()所有内容读入内存
#readlines()所有内容读入内存,按照行进行读取,存为列表
#读
my_list= []
line_list = file_obj.readlines()
for i in line_list:
line =  i.strip()
print line
value_list = line.split(';')
print value_list
last_value = int(value_list[-1])
last_value += 1
value_list[-1] = str(last_value)
print value_list
value_str = ';'.join(value_list)
my_list.append(value_str)
print my_list
file_obj.close()
#写
#writeline()一行写
my_str = '\n'.join(my_list)
print my_str
file_obj = file('1.txt','w')
file_obj.write(my_str)

#关闭文件
file_obj.close()

9.练习

#!/usr/bin/env python
# encoding: utf-8

#输入用户名和密码
#错误三次锁定用户
i=0
while i < 3:
name = raw_input("please input name:")
pwd = raw_input('please input password:')

if name == 'alex' and pwd == '123':
print 'login success'
else:
print 'login failed'
i += 1
print
#多级菜单 选择省份----市级-----县级
def show():
p_list = ['sichuan','guangdong']

sc_list = ['chengdu','deyang','mianyang',]
cd_list = ['dayi','qionglai']
dy_list = ['zhongjiang','shifang']
my_list = ['zitong','jiangyou']
gd_list = ['guangzhou','foshan','meizhou',]
gz_list = ['baiyun','haizhu','tianhe']
fs_list = ['gaoming','heshan','sanshui']
mz_list = ['xingning','dapu','pingyuan']
print p_list
p_no = raw_input('please choose:')
if p_no == p_list[0]:
print sc_list
c_no = raw_input('please choose:')
if c_no == sc_list[0]:
print cd_list
elif c_no == sc_list[1]:
print dy_list
elif c_no == sc_list[2]:
print my_list
elif p_no == p_list[1]:
print gd_list
c_no = raw_input('please choose:')
if c_no == gd_list[0]:
print gz_list
elif c_no == gd_list[1]:
print fs_list
elif c_no == gd_list[2]:
print mz_list

if __name__ == '__main__':
while True:
print show()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 基础