python 笔记2:python语法基础
python语法学习笔记:
1 输入输出 input(),print()。
name = input('input your name : ')
print('hello ,'+name)
print(" I'm python "*3)
print('{}+{}={}'.format(1,2,1+2))
print(1,2,['x','y'],'a')
print(type('1'),type(1))
print(len(name))
运行结果:

2 打开文件,写入数据。
file=open('D:/Program Files/dinghanhua/file.txt','w')
file.write('this is a file.\n the second line.')
file.close
print('Done!')
with open('Path/file1.txt','r') as f1:
for line in f1:
print(line)
运行结果:

3 字符串切片,查找,按空格分离
name='My name is Mike'
print(name[0])
print(name[-4])
print(name[11:14])
print(name[5:])
print(name[:5])
print(name[-3:-1])
account='13800001234'
hiding_number=account.replace(account[:7],'*'*7)
print(hiding_number)
num1='13588823888'
search='23'
print(num1.find(search))
import string
list='where is your bag? \n I am finding it.'
print(list.split())
print([word.strip(string.punctuation).lower() for word in list.split()])
运行结果:


4 函数
def third(a=1,b=1):
c=(a**2+b**2)**(1/2)
return(c)
print(third(3,4))
5 条件判断 if ... elif... else:
n=input('your choice: 1,2,3')
if n in ['1','2','3']:
if int(n)==1:
print('service 1')
elif int(n)==2:
print('service 2')
else:
print('service 3')
else:
print('invalid input.')
6 循环 for,while
显示乘法口诀表:
for i in range(1,10):
for j in range(1,i+1):
print( '{}*{}={} '.format(j,i,i*j),end='')
print()



7 其他:
程序包含中文,在最上面加上注释:# -*- coding=utf-8 -*-
列表:list=[1,2,'a',['x']] 列表和字符串一样可以切片
list=[1,2,'a',['x']]
print(list[0],list[2:],list[:3])
字典:
dict={'key1':'value1','key2':'value2'}
print(dict['key1'])
集合:元素无重复,不能用索引
set={1,3,2,3,4,5,'1'}
print(set)
元祖:tuple
tuple=(1,'a','b',2)
tuple=(1,'a','b',2)
print(tuple[0])
python 笔记2:python语法基础的更多相关文章
- python笔记之中缀语法和管道实现
python笔记之中缀语法和管道实现 你知道什么是中缀语法吗?你知道python中的中缀操作是什么吗?那你知道操作python也是可以像unix的管道符一样方便吗?那么,废话不说了,直接上代码. cl ...
- 【Python笔记】Python 基础语法
Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...
- 【Python笔记】Python语言基础
Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...
- python笔记之python基础
python基础语法 1.变量命名规则 1).变量必须以字母或者下划线_开头 2).变量可由字母.数字.下划线_组成 3).变量对大小写敏感 2.多行语句 当编写的代码较长,需要换行,可使用 \ 进行 ...
- python运行以及入门语法基础
pycharm下载与使用 1.pycharm官网下载(直接到pycharm下载地址) http://www.jetbrains.com/pycharm/download/#section=window ...
- python笔记(1)--基础知识
一.注释 单行注释 #打印“hello world” print("hello.world!") 另外一种单行注释 print("hello,world!") ...
- guxh的python笔记二:函数基础
1,函数的参数 1.1,查看函数的参数类型 def run(a, *args, b, **kwargs): return a + b 可以通过如下方式查看参数类型: import inspect k ...
- python笔记1,语法,函数,类和实例,异常
>>> int(12.34) 12 >>> float('12.34') 12.34 >>> str(1.23) '1.23' >>& ...
- Python笔记_初级语法
1.标识符与变量 1.1 标识符 规范 只能由数字,字母,_(下划线)组成 不能以数字开头 不能是关键字 区分大小写 命名约束 下划线分隔法(推荐): 多个单词组成的名称,使用全小写字母书写,中间使用 ...
- 【Python笔记】Python变量类型
Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...
随机推荐
- scss/css 中添加ie hack
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) { /* IE10+ specific styles ...
- PBOC~PPT-补充内容B(转)
PBOC电子现金基于借记/贷记应用上小额支付的一种实现.在借记卡上可以解释为预付,在贷记卡上可以解释为预先授权.预付的金额或预授权额度在卡片中体现为可脱机消费的金额,也就是电子现金余额.电子现金解决方 ...
- ComboboxColumn取值——Winform中DataGridView中某一列使用下拉框
ComboboxColumn的用法网上很多,绑定数据源都很简单,这里我遇到的是.不绑定数据源,即所有comobox的绑定都是固定的几个数据: 可以看到没有绑定任何数据源, ,在后台cs中取到下拉框的值 ...
- js json 特定条件删除 增加 遍历
<script type="text/javascript"> //直接声明json数据结构 var myJSONObject = [ ...
- Python正则化学习
- 深入理解javascript系列,读书笔记
深入理解JavaScript系列(2):揭秘命名函数表达式 1.讲了函数声明和函数表达式的区别,包括一些在函数提升上的区别 2.如果给函数表达式的函数也取名,会在调试的时候受益 3.不要在block( ...
- 内存分配与Segmentation fault
为了方便使用,我做了如下结构体的嵌套使用: struct operation{ int num; char name[100]; char owner[100]; char msg[100];}; s ...
- Spring MVC常用注解
cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...
- UISegmentControl
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- file_put_contents保存数据,
file_put_contents("sui.txt", var_export($now_member,TRUE),FILE_APPEND); 可以保存数组,方便调试