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. UNITY3D ShadeSH9

    UNITY3D ShadeSH9 属于Irradiance environment maps 方法,可以参考DX SDK PRTDemo,里面是几乎相同的实现,总之就是解光传输的积分方程 目前主流辐射 ...

  2. BFS 10.1.5.253 1502

    http://10.1.5.253/acmhome/problemdetail.do?&method=showdetail&id=1502 //1502 #include <st ...

  3. 《University Calculus》-chaper13-向量场中的积分-线积分

    线积分: 基于二重积分和三重积分的引入,我们对于线积分的引入过程将会轻车熟路. 对于一根不均匀密度的铜丝,我们如何求其总质量?如下图. 类似二重积分和三重积分的引入,我们首先基于实际问题给出黎曼和的形 ...

  4. centos 安装node js环境

    node.js支持多种平台安装,其中Win平台安装比较简单,下面重点讲解下Linux平台的安装步骤.本文以CentOS平台为实例,不准备讲 解采取源码编译安装方式,而是采取在node.js网站下载已经 ...

  5. 上海Uber优步司机奖励政策(1月18日~1月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. B - The Suspects -poj 1611

    病毒扩散问题,SARS病毒最初感染了一个人就是0号可疑体,现在有N个学生,和M个团队,只要团队里面有一个是可疑体,那么整个团队都是可疑体,问最终有多少个人需要隔离... 再简单不过的并查集,只需要不断 ...

  7. mysql忘记root密码 + 授权登录

    一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p password "test123"E ...

  8. MySQL删除重复记录的方法

    参考网上的方法,总结了产出重复记录的方法,欢迎交流. 参考:http://www.cnblogs.com/nzbbody/p/4470638.html 方法1:创建一个新表临时储存数据 假设我们有一个 ...

  9. Spark GraphX的函数源码分析及应用实例

    1. outerJoinVertices函数 首先给出源代码 override def outerJoinVertices[U: ClassTag, VD2: ClassTag] (other: RD ...

  10. Swoole源代码学习记录(十五)——Timer模块分析

    swoole版本号:1.7.7-stable Github地址:点此查看 1.Timer 1.1.swTimer_interval_node 声明: // swoole.h 1045-1050h ty ...