1.数字:int(整型)

32位机器:-2**31~2**31-1

64位机器:-2**63~2**63-1

float(浮点型)

2.布尔值

真或假

1或0

bool(0)

3.字符串

name = “wanghuafeng”

print(“my name is ”+ name + “and you?”)

万恶的加号,开辟多块内存空间。

4.列表

创建列表:

name_list = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc']
取出第一个元素:
name_list[0]
切片
注意:切片顾头不顾尾,并且从左往右
   取出第一个和第二个元素:
name_list[0,2]
   取后3个元素:
name_list[-3:]
  取前3个元素:
name_list[:3]
  多次切片:
name_list[:3][:2][0][3]
修改:
name_list[2]  = 'git'
插入(一次只能插一个):
name_list.insert(2,'svn')
追加:
name_list.append('lilei')
删除:
name_list.remove('dc')
删除第2个元素
name_list.remove(name_list[1])
删除第2第3个元素
del name_list[1:3]
删除列表
del name_list
打印奇数个元素(步长)
name_list[::2]
判断元素在该列表中:
name_list = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
if 2 in name_list:
print("2 is in name_list")
统计列表中相同元素个数:
name_list = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
if 2 in name_list:
num_of_ele = name_list.count(2)
print("%s个2 is /are in name_list" % num_of_ele)
查找元素在列表中的位置
name_list = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
if 2 in name_list:
num_of_ele = name_list.count(2)
position_of_ele = name_list.index(2)
print("%s个2 is /are in name_list, position is %s" % (num_of_ele,position_of_ele))

找到元素并修改

name_list = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
if 2 in name_list:
num_of_ele = name_list.count(2)
position_of_ele = name_list.index(2)
name_list[position_of_ele] = 888
print("%s个2 is /are in name_list, position is %s" % (num_of_ele,position_of_ele))
print(name_list)
修改列表中相同的元素
name_list = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
if 2 in name_list:
num_of_ele = name_list.count(2)
position_of_ele = name_list.index(2)
name_list[position_of_ele] = 999
print("%s个2 is /are in name_list, position is %s" % (num_of_ele,position_of_ele))
print(name_list)

for i in range(name_list.count(2)):
ele_index = name_list.index(2)
name_list[ele_index] = 988
print(name_list)
扩展列表
name1 = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
name2 = ['zhangsan', 'lisi', 'wangxiao', 2, 2, 3, 4, 5]

name1.extend(name2)
print(name1)
print(name2)
反转列表
name1 = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
name1.reverse()
print(name1)
排序
python3中数字和字符串不能一起排序
python2中按照ascii码的顺序排序,一般数字在前
name1.sort()
取出列表中的元素
name = ['wanghuafeng', 'alex', 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7
取出列表中的3个元素
list1 = name.pop(2)
默认取出最后一个元素
name.pop()
print(name)
print(list1)
列表复制
name = ['wanghuafeng', 'alex', [1, 1, 5, 8, 9], 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
name1 = name.copy()
print(name)
print(name1)
name[0] = 'WANG'
print(name)
print(name1)
嵌套列表
name = ['wanghuafeng', 'alex', [1, 1, 5, 8, 9], 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
name1 = name.copy()

name[0] = 'WANG'
name[2][1] = 88888888
print(name)
print(name1)
name1[2][3] = 99999999
print(name)
print(name1)
深copy和浅copy
浅copy:name.copy默认只拷贝第一层,嵌套的列表不copy,嵌套的列表指向了其它内存地址,copy只是引用了其地址相当于软连接
深copy:name3.deepcopy(name)
import copy
name = ['wanghuafeng', 'alex', [1, 1, 5, 8, 9], 'eric', 'snow', 'radar', 'dc', 2, 2, 2, 3, 4, 5, 6, 7]
name1 = name.copy()

name[0] = 'WANG'
name[2][1] = 88888888
print(name)
print(name1)
name1[2][3] = 99999999
print(name)
print(name1)
浅copy
name2 = copy.copy(name)
print(name2)
深copy
name[2][4] = 222222
name3 = copy.deepcopy(name)
name3[2][4] = 33333333333
print(name)
print(name3)
查看列表长度
print(len(name3))
元组
r = (1, 2, 3, 4, 5, 1, 1, 2)
查看类型
print(type(r))
统计元组中1的个数
print(r.count(1))
查找元组中2的位置
print(r.index(2))
字符串
去掉前后空格
user_name = input("user:")
if user_name.strip() == 'wang':
print("Welcome!")
分割
names = " wang,hua,feng"
name5 = names.split(',')
print(name5)
合并
names = " wang,hua,feng"
name5 = names.split(',')
print(name5)

print('|'.join(name5))
判断是否有空格
name = 'wang hua feng'
print('' in name)
首字母大写
name = 'wang hua feng'
print(name.capitalize())
格式化
name = "hello, {name}, it's been a long {age} time since last time spoke..."
name_msg = name.format(name="ahha", age=25)
print(name_msg)
name2 = "haha{0}, dddddssss{1}"
print(name2.format('wang',23))
切片
name = 'wanghuafeng'
print(name[2:8])
填充
name = 'wanghuafeng'

print(name.center(29,'*'))
查找
name = 'wanghuafeng'
print(name.find('a'))
判断是否是数字
age  = input("your age:")
if age.isdigit():
age = int(age)
print("age is %s" % age)
else:
print("Invalid date type")
判断是否有数字,但不能有特殊字符
name = 'aaaas2ss2ssd'
print(name.isalnum())
是否以sd结尾
name = 'aaaas2ss2ssd'

print(name.endswith('sd'))
是否以aa开头
name = 'aaaas2ss2ssd'
print(name.startswith('aa'))
大小写转换
name = 'aaaas2ss2ssd'
print(name.upper())
print(name.lower())

算数运算

+    -    *     /     %    **    //

取商

print(11/2)
取模
print(11%2)
取商的整数部分
print(13//2)

比较运算

==    >     <     >=   <=

赋值运算

=    +=    -=    *=    /=    %=    **=    //=

逻辑运算

and    or    not

成员运算

in    not in

身份运算

is    is not

type(a) is list

位运算

&:按位与

|:按位或

^:按位异或

~:按位取反

<<:左移

>>:右移

计算机中能表示的最小单位,是一个二进制位;计算机中能存储的最小单位,是一个二进制位(bit)

8bit = byte(字节)

1024byte = 1kbyte

1024kbyte = 1mbyte

1024mbyte = 1gb

1024gb = 1T

while循环

循环100次

count = 0
while True:
print("哈哈")
count += 1
if count == 100:
print("结束")
break
50-60之间不打印
count = 0
while True:
count += 1
if count > 50 and count < 60:
continue
print("哈哈 ...%s" % count)
if count == 100:
print("结束")
break
数据字典
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: WangHuafeng

id_db = {
421087198901298697: {
'name': 'radar',
'age' : 22,
'addr':'HuBei',
},
421087154671298697: {
'name': 'snow',
'age': 22,
'addr': 'AnHui',
},
421087133221298697: {
'name': 'fish',
'age': 10,
'addr': 'Shanxi',
},
}
#自动去重,key需唯一
print(id_db)
print(id_db[421087133221298697])
#修改
id_db[421087133221298697]['name'] = "NiuRen"
print(id_db[421087133221298697])
#删除
id_db.pop(421087133221298697)
print(id_db)
id_db[421087154671298697].pop("addr")
print(id_db)
#copy复制
#clear清空
#get获取
v = id_db.get(421087198901298697)
print(v)
#update
dict1 = {
'name':'Bottle',
421087154671298697: {
'name': 'radar',
},
}
id_db.update(dict1)
print(id_db)

print(id_db)
#id_db.items()把字典转换为元组,数据量大时不要使用
print(id_db.items())

dict_values = id_db.values()
dict_keys = id_db.keys()
print(dict_keys)
print(dict_values)
421087154671298697 in id_db #eauals to above has key(x)

id_db.setdefault(421087154671298697,"haha") #取一个值key,如果不存在就设置一个值;如果存在直接设置值
print(id_db)

print(dict.fromkeys([1,2,3,4,5]),'bfgb') #把字典里的所有值都当作key

print(id_db.popitem()) #随机删除,尽量不要用

for k,v in id_db.items(): #效率低,需要有一个字典转换为列表操作
print(k)
print(v)
for key in id_db: #效率高
print(key,id_db[key])

购物车
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: WangHuafeng

#输入工资一次性
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
else:
exit("Invalid date type...")

welcome_msg = 'Welcome to Wang Shopping mall.'.center(50,'-')
print(welcome_msg)

product_list = [
('iphone', 5800),
('Mac Air', 8000),
('xiaomi', 19.9),
('coffee', 30),
('Tesla', 820000),
('Bike', 900),
]
exit_flag = False
print("Product list.".center(50,'-'))
shop_car = []
while not exit_flag: #while exit_flag is not True
for item in enumerate(product_list): #enumerate枚举
#p_name, p_price = product_item
#print(p_name, p_price)
#print(item)
index = item[0]
p_name = item[1][0]
p_price = item[1][1]
print(index,'.',p_name,p_price)

user_choice = input("[q=quit,c=check]What do you want to buy : ")
if user_choice.isdigit(): #肯定选择shopping
user_choice = int(user_choice)
if user_choice < len(product_list):
p_item = product_list[user_choice]
if p_item[1] <= salary: #买得起
shop_car.append(p_item) #加入购物车
salary -= p_item[1] #扣钱
print("Added [%s] into shop car,you current balance is \033[31;1m][%s]\033[0m" % (p_item, salary))
else:
print("Your balance is [%s], cannot afford this." % salary)
else:
if user_choice == 'q' or user_choice == 'quit':
print("purchased products as below".center(40,'*'))
for item in shop_car: #打印已购买的商品
print(item)
print("END".center(40,'*'))
print("Your balance is \033[41;1m][%s]\033[0m" % salary)
print("Bye")
exit_flag = True
elif user_choice == 'c' or user_choice == 'check':
print("purchased products as below".center(40, '*'))
for item in shop_car: # 打印已购买的商品
print(item)
print("END".center(40, '*'))
print("Your balance is [%s]" % salary)

Day2 数据类型的更多相关文章

  1. Day2 数据类型和运算符

    基本数据类型 Java 是一种强类型的语言,声明变量时必须指明数据类型.变量(variable)的值占据一定的内存空间.不同类型的变量占据不同的大小.Java中共有8种基本数据类型,包括4 种整型.2 ...

  2. DAY2:数据类型Python3.6

    数字 1.int(整型) 2. long(长整型) 3.float(浮点型) 4.complex(复数)  布尔值 1.真或假 1或0表示 字符串 知识补充 字符串转二进制 mes = "北 ...

  3. python基础一 day2 数据类型

    int:        bool: 类型转换: str到int有条件,str必须是数字, "123e"是错误的 bool转换为int类型,需要int(x)  结果:  结果: 空字 ...

  4. 【目录】Python自动化运维

    目录:Python自动化运维笔记 Python自动化运维 - day2 - 数据类型 Python自动化运维 - day3 - 函数part1 Python自动化运维 - day4 - 函数Part2 ...

  5. 跟着ALEX 学python day2 基础2 模块 数据类型 运算符 列表 元组 字典 字符串的常用操作

    声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/  模块初始: Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相 ...

  6. python day2:python的基本数据类型及其方法

    目录 python day2 1. 编码转换 2. python的基本数据类型 3. for 迭代遍历 4. 列表list 5. 元组tuple 6. 字典dict 7. 枚举enumerate 8. ...

  7. day2. 六大基本数据类型简介

    一.基本数据类型 Number 数字类型 (int float bool complex) str 字符串类型 list 列表类型 tuple 元组类型 set 集合类型 dict 字典类型 二.Nu ...

  8. Day2 基本数据类型

    一.python数据类型 1.1数字 2 是一个整数的例子. 长整数 不过是大一些的整数. 3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4 ...

  9. Python day2 基础 2 数据类型

    数据类型初识 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j)和 ...

随机推荐

  1. 暴力求解——POJ 3134Power Calculus

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  2. POJ2286 The Rotation Game(IDA*)

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 5691   Accepted: 19 ...

  3. 例6.1:学生选课系统设计(界面设计、类图、数据库ER图)

  4. UVALive 5990 Array Diversit

    题意:对于一个数列A,substring是一个连续子串,subsequence是其非连续子序列.对于一个数字序列,记它的diversity是它的最大元素减去最小元素的差.给出一个数字序列,求与它div ...

  5. Postman interceptor

    安装 下载地址: Postman Interceptor Chrome插件下载 1. 下载的是一个crx文件. 2. 在谷歌中打开: chrome://extensions/ 3. 拖动cfx文件到 ...

  6. Android系统默认Home应用程序(Launcher)的启动过程源码分析

    在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还须要有一个Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home应 ...

  7. JQ插件之imgAreaSelect实现对图片的在线截图功能(java版)

    前言:在做网站的时候经常用的功能就是,用户上传图片对自己上传的图片进行截图,DIV自己的头像.或者上传幻灯片大图进行DIV设置小图. 解决方案:目前我知道的解决方案有两个如下:       一.fla ...

  8. 如何写好一个UITableView

    本文是直播分享的简单文字整理,直播共分为上.下两部分. 第一部分: 优酷 :http://v.youku.com/v_show/id_XMTUzNzQzMDU0NA%3Cmark%3E.html Or ...

  9. 转载:浅析C#深拷贝与浅拷贝

    原文地址 :http://www.cnblogs.com/xugang/archive/2010/09/09/1822555.html   感谢博主分享! 也许会有人这样解释C# 中浅拷贝与深拷贝区别 ...

  10. OD: Kernel Exploit - 1

    第 22 章,内核漏洞利用技术 首先编写具有漏洞的驱动 exploitme.sys,再展开内核漏洞利用思路和方法: /***************************************** ...