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语法基础的更多相关文章

  1. python笔记之中缀语法和管道实现

    python笔记之中缀语法和管道实现 你知道什么是中缀语法吗?你知道python中的中缀操作是什么吗?那你知道操作python也是可以像unix的管道符一样方便吗?那么,废话不说了,直接上代码. cl ...

  2. 【Python笔记】Python 基础语法

    Python 标识符 在 Python 里,标识符由字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大小 ...

  3. 【Python笔记】Python语言基础

    Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...

  4. python笔记之python基础

    python基础语法 1.变量命名规则 1).变量必须以字母或者下划线_开头 2).变量可由字母.数字.下划线_组成 3).变量对大小写敏感 2.多行语句 当编写的代码较长,需要换行,可使用 \ 进行 ...

  5. python运行以及入门语法基础

    pycharm下载与使用 1.pycharm官网下载(直接到pycharm下载地址) http://www.jetbrains.com/pycharm/download/#section=window ...

  6. python笔记(1)--基础知识

    一.注释 单行注释 #打印“hello world” print("hello.world!") 另外一种单行注释 print("hello,world!") ...

  7. guxh的python笔记二:函数基础

    1,函数的参数 1.1,查看函数的参数类型 def run(a, *args, b, **kwargs): return a + b 可以通过如下方式查看参数类型: import inspect k ...

  8. python笔记1,语法,函数,类和实例,异常

    >>> int(12.34) 12 >>> float('12.34') 12.34 >>> str(1.23) '1.23' >>& ...

  9. Python笔记_初级语法

    1.标识符与变量 1.1 标识符 规范 只能由数字,字母,_(下划线)组成 不能以数字开头 不能是关键字 区分大小写 命名约束 下划线分隔法(推荐): 多个单词组成的名称,使用全小写字母书写,中间使用 ...

  10. 【Python笔记】Python变量类型

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

随机推荐

  1. 萝卜招聘网 http://www.it9s.com 可以发布免费下载简历求职 ,免费!免费!全部免费!找工作看过来 免费下载简历 !

    萝卜招聘网  http://www.it9s.com  可以发布免费下载简历求职 ,免费!免费!全部免费!找工作看过来 免费下载简历 !萝卜招聘网  http://www.it9s.com  可以发布 ...

  2. TestNG BeforeClass BeforeMethod Test AfterClass AfterMethod

    http://topmanopensource.iteye.com/blog/1983729 1.TestNG测试注解和Junit注解的不同以及生命周期: TestNG测试的一个方法的生命周期: @B ...

  3. java, listmap2json, fastjson

    import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;impor ...

  4. 在线调试lua原型设计

    在规模日益增长的软件项目开发中, 如何有效的进行代码调试成为影响开发效率的致命因素之一.在当今网络游戏项目中, lua几乎成了项目脚本的标配.编译型的语言, 诸如C++, 都有良好的ide支持调试.而 ...

  5. hadoop中遇到的一些问题

    1.验证词统计程序.----无法加载本地库 出现错误: WARN util.NativeCodeLoader: Unable to load native-hadoop library for you ...

  6. MySQL TCL 整理

    TCL(Transaction Control Language)事务控制语言SAVEPOINT 设置保存点ROLLBACK  回滚SET TRANSACTION

  7. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  8. 11.10 Taolu1234组信息汇总

    团队名称: Taolu1234 团队选题: <餐厅到店点餐系统>WEB版 团队博客地址: http://www.cnblogs.com/queenjuan/ 团队GITHUB地址: htt ...

  9. nodejs 服务端添加相应头Access-Control-Allow-Origin

    重点在这句:res.setHeader("Access-Control-Allow-Origin", "*"); var http = require(&quo ...

  10. 面试中常用的__proto__,prototype和原型链,你都了解了吗?

    上一篇随笔主要讲了变量提升的问题,今天我们来讲讲很多前端er在初期很长一段时间内都没有完全搞明白的原型链和构造函数. 1,什么是构造函数 那么要讲到构造函数,必须要有一个函数,所以我们建立一个函数 f ...