Python之路,day2-Python基础1
python2
range(20)
for i in range(10):
print(i)
range(1,10) -----》从1开始到9
else: #如果for循环正常结束, 就执行else语句,(break为不正常结束)(注:此处的的else为与for循环的for同级别)
break #跳出整个当前循环,只跳一层
continue#跳出当次循环,继续下一次循环
while循环
count = 0
while Ture:
if count = 10000000
print('dsdsfdsfsf')
break
count+=1
count = 0
while True:
if count == 10000000
print('dsdsfdsfsf')
break
count+=1
count = 0
while count < 100:
print('dsdsfdsfsf')
count+=1
变量: 用来记录状态
变量值的变化即状态的变化, 程序运行的本质就是来处理一系列状态的变化
数据类型(五大基础数据类型)
1.数字
整型int
base 用来把字符串转换为10进制的整数
int('0b1010',base=2)
age=10---->int(10) ------>_init_
长整型
布尔bool
ture 和 false
1和0
浮点数float
复数
2.字符串
msg='hello world'---->str
print (msg[1])字符串字母特定位置
print(msg.capitalize())#首字母大写
print(msg.center(20,‘*’)) 定义居中格数,以及空格处符号(默认为空格)
print(msg.count(‘l’,4,7)) # -1(代表从右边数第一个,相当于10)
print(msg.endswitch('l') ) #结尾的字母是否为‘l’
msg1='a\tb'
print(msg1.expandtabs(10))#指定tab的空格数,默认4个
print(msg.find('d'))#返回元素在字符串的位置,如果同一元素有多个找到第一个就结束,后面木再找
print(msg.find('d',0,4))
format
print('{0}{1}'.format('name','age'))
print('{name}'.format(name='alex'))#调用后面的定义变量的值
print('{}{}'.format('name','age'))#一一对应
字符串判断
msg3=‘a123’
print(msg3.isalnum())#字幕和数字组成的字符串
print(msg3.isalpha())#全是字幕返回true
msg4=‘10’
msg5='10.2'
print(msg5.isdecimal)#浮点数
msg6='10'
print(msg.isdigit())判断是否为整型
msg7='10.3'
print((msg7).isnumeric)
msg8='while'
print(msg.isidentifier)
msg9=‘aaa’
print(msg.islower) #小写字母
msg10=‘ aaa’
print(msg10.isspace)#包含空格
msg11=‘Hello’
print(msg11.istitle)#单词首字母大写为title
msg12=‘Hello’
print(msg12.isupper)#全是大写
msg13=‘abc’
print(msg13.ljust(10,'*'))#左对齐
print(msg13.rjust(10,'*'))# 右对齐
print(msg13.center(10,'*'))# 居中
print(msg13.upper(10,'*'))# 将小写转化为大写
字符串常用的
# str()
# msg='hello world'
# print(msg.capitalize())
#
#
# print('{0} {1}'.format('name','age'))
#
# print('{name}'.format(name='alex'))
# print('{}{}'.format('name','age'))
#
# print(msg.endswith( 'l')) # msg13='abc'
#
# print(msg13.ljust(10,'*'))
#
# print(msg13.rjust(10,'*'))
#
# print(msg13.center(10,'*'))
#
# print(msg13.upper(10,'*'))
#
#
# #==================================
#
# msg14='hello'
# print(msg14.find('w'))
# print(msg14.index('w'))
#
# msg15=' sdff '
# print(msg15.strip())#去掉首尾的空格
# print(msg15.lstrip())#去掉左边的空格
# print(msg15.rstrip())#去掉右边的空格
#
# #制造翻译表
# msg16='my name is abc'
# table=str.maketrans('abc','ale')
# print(msg16.translate(table))
# #
# #zfill
# msg17='abc'
# print(msg17.zfill(20))#右对齐,不够的用‘0’补
# print(msg17.ljust(20,'0'))
# print(msg17.rjust(20,'0')) #字符串常用的操作
#移除空白
# msg19='123234423423'
# print(msg19.strip('1'))
# #分割 ----取范围
# msg20='nihao 123'#
# print(msg20[0:3])
# print(msg20[2:7:2]) #隔两个取一次
# #长度
# len(msg20)#字符串长度
# len(msg20)/2
#
# round(len(msg20)/2)
# #索引 下表
# #切片 分割
#
#
# #==================
# #运算符
# #1.算数运算符 +-*/
# #‘//‘地板除,只取整数部分
# #2.比较运算符
# # == !=
# 赋值运算符
# #age+=1<==> age=age+1
#
# 位运算符
#
# 逻辑运算
#
# 成员运算
#
# 身份运算
#
# count = 0
# while True:
# if count == 10000000
# print('dsdsfdsfsf')
# break
# count+=1 # count = 0
# while count < 100:
# print('dsdsfdsfsf')
# count+=1 # age = 20
# count = 0
# while count < 3:
# myage = input('myage:')
# if myage.isdigit():
# myage = int(myage)
# else:
# continue
# if myage == age:
# print('yes')
# break
# elif myage < age:
# print('猜大点')
# else:
# print('猜小点')
# count+=1 #列表 names = ['a','b','c','d'] #zeng
# names.append('e')
# print(names)
#
# names.insert(2,'f')
# names.insert(1,'g')
# print(names)
#
# #shan
# names.remove('f')
# print(names)
#
# del names[1]
# print(names)
#
# names.pop(3)
# print(names)
#
#
# #gai
# names[2] = 'k'
# print(names)
#
# #cha
# print(names[-2])
# print(names[0::2])
# print(names[-3:])
#
# print( names.index('s') )
#
first_index = names.index('a')
second_index = names[first_index + 1:].index('b')
print('second ',second_index+first_index+1) print('count',names.count('b'))
n2 = ['5']
names.extend(n2) names.reverse()
names.sort() print(names) n3 = names.copy()
print(n3) n4= names names.pop() #打印下表和值
for i,ele in enumerate(names):
print(i,ele)
3.列表
练习题
购物车程序
your salary>>:5000
-------shop list------
1.iphone 5800
2.macbook 12800
3.coffee 30
4.bike 2000
------end-----
>>:1
钱不够
>>:3
added [coffee]into your shopping list,your current balance is 4970
>>:
>>:quit
your balance is 4000
已购买商品
1.coffee 30
Python之路,day2-Python基础1的更多相关文章
- Python之路,Day2 - Python基础(转载Alex)
Day2-转自金角大王 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存 ...
- Python之路,Day2 - Python基础2
def decode(self, encoding=None, errors=None): """ 解码 """ ""& ...
- Python之路 day2 文件基础操作
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #f,文件句柄;模式 a : append 追加文件内容 f = open( ...
- Python之路,Day2 - Python基础,列表,循环
1.列表练习name0 = 'wuchao'name1 = 'jinxin'name2 = 'xiaohu'name3 = 'sanpang'name4 = 'ligang' names = &quo ...
- Python之路,Day4 - Python基础4 (new版)
Python之路,Day4 - Python基础4 (new版) 本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...
- Python之路,Day1 - Python基础1
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Python之路,Day1 - Python基础1(转载Alex)
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Python之路,Day1 - Python基础1 --转自金角大王
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Python之路,Day1 - Python基础1 介绍、基本语法、流程控制
本节内容 1.python介绍 2.发展史 3.python 2.x or python 3.x ? 4.python 安装 5.第一个程序 Hello World 程序 6.变量 7.用户输入 8. ...
- Python之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
随机推荐
- ie兼容问题整理
1.连续发请求问题 * jquery(document).ready(function(){}) * 连续发请求ie8出问题,被拦截问题,url后边加时间戳 * 例 url : url+ ...
- java高薪之路__001_类
Java中内部类分四种:成员内部类.局部内部类.静态内部类和匿名内部类.要注意静态内部类的调用方式与其他不同,采用的是类似调用类中的静态属性.静态方法的方式 Multi Level 调用不同类中的相同 ...
- Spring + Mybatis 使用 PageHelper 插件分页
原文:http://www.cnblogs.com/yucongblog/p/5330886.html 先增加maven依赖: <dependency> <groupId>co ...
- easyui-panel 滚动条禁用
div id="p" class="easyui-panel" title="title" style="padding:10px ...
- [SoapUI] Post请求Body里面限制特殊字符(&、%),Groovy脚本里特殊字符需要添加“\”转义($)。
SoapUI的Post请求,在body里面不能包含(&.%),如果含有这些特殊字符,请求会报错:在添加的Groovy脚本里有些特殊字符需要使用“\”转义($),不然也会报错.
- MDK的一些设置(尤其是部分代码无法断点问题的解决)
转自http://www.cnblogs.com/worldsing/p/3412323.html
- Evolutionary Computing: 3. Genetic Algorithm(2)
承接上一章,接着写Genetic Algorithm. 本章主要写排列表达(permutation representations) 开始先引一个具体的例子来进行表述 Outline 问题描述 排列表 ...
- linux笔记:shell基础-概述和脚本执行方式
什么是shell: linux使用的默认shell是Bash: shell脚本的后缀名为.sh,shell脚本的第一行#!/bin/bash 不是注释,而是标识这是一个shell脚本,因为linux并 ...
- RAID与双机热备简单介绍与区别
一. RAID技术详解 RAID是英文Redundant Array of Independent Disks的缩写,翻译成中文意思是“独立磁盘冗余阵列”,有时也简称磁盘阵列(Disk Arra ...
- PHP自动发邮件
自动发邮件 使用了这个类http://bbs.php100.com/read-htm-tid-121431.html 因他用的php版本较老,用到了函数ereg_replace() 和 ereg() ...