# 廖雪峰的官方网站 python教材 1~4章

# 格式控制符语法

print('Hello, %s' % 'world')
print('hello, %s, you have %d dollars' % ('mickael', 1000))
print('hello, {0:s}, your grade {1:.1f}%'.format('xiaoming', 17.125)) s1 = 72
s2 = 85
improve = (s2-s1)/s1 * 100
print('hello, {0:s}, your grade improved by {1:.1f}%'.format("xiaoming", improve)); # 列表 list classmate = ['xiaoming','xiaozhao','xiaohong']
print(classmate) print(len(classmate)) print(classmate[1]) classmate.insert(1,"Jack")
print(classmate) classmate.pop()
print(classmate) classmate.pop(1)
print(classmate) classmate[1] = "Sarah"
print(classmate) L = ['apple', 123, True]
print(L) # 判断 age = 3
if age>= 18:
print("adult") elif age >= 6:
print("teenager")
else:
print("kids") print("your age is", age) # 转换成 int birth = input('birth: ')
birth = int(birth)
if birth < 2000:
print("00qian")
else:
print("00hou") # 循环 sum = 0
for x in list(range(5)):
sum = sum + x
print(sum) alphobets = ['a','b','c']
for char in alphobets:
print(char) names = ['michael', 'bob', 'Tracy']
for name in names:
print(name) sum = 0
i = 0
while(i<101):
sum += i
i += 1
print(sum) L = ['Bart', 'Lisa', 'Adam']
for name in L:
print("hello, %s!" % name) # 字典 dictionary d = {'Michael':95, 'Bob':75, 'Tracy':85}
d['Adam'] = 67
print(d['Adma']) # 函数 def myabs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x>=0:
return x
else:
return -x print(myabs(111)) # 函数返回元组 import math def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y + step * math.sin(angle)
return nx, ny # 分别赋给每个变量 x, y = move(100,100,60,math.pi/6)
print(x,y) def quadratic(a,b,c):
delta = b*b - 4*a*c
x1 = (-b + math.sqrt(delta)) / (2 * a)
x2 = (-b - math.sqrt(delta)) / (2 * a)
return x1, x2 print('quadratic(2, 3, 1) =', quadratic(2, 3, 1))
print('quadratic(1, 3, -4) =', quadratic(1, 3, -4)) if quadratic(2, 3, 1) != (-0.5, -1.0):
print('测试失败')
elif quadratic(1, 3, -4) != (1.0, -4.0):
print('测试失败')
else:
print('测试成功') #def power(x):
# return x * x # 默认参数 def power(x,n=2):
s = 1
while n > 0:
n = n - 1
s = s * x
return s print(power(5))
print(power(5,3)) # 可变长度参数 def clac(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum print(clac(1,2))
print(clac()) # 递归:汉诺塔
# 参数n,表示初始时,3个柱子a、b、c中第1个柱子a上的盘子数量,
# 打印出把所有盘子从A借助B移动到C的方法
def move(n, a, b, c):
if n == 1:
print(a, '-->', c)
else:
move(n-1,a,c,b)
print(a,'-->',c)
move(n-1,b,a,c)
return move(3,'A','B','C')

python learning1.py的更多相关文章

  1. python调用py中rar的路径问题。

    1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...

  2. python gettitle.py

    #!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...

  3. Python pydoc.py

    1. 查看帮助,我们可以在python命令行交互环境下用 help函数,比如: 查看 math 模块: >>> help('math')Help on built-in module ...

  4. django 1.7之后python manage.py syncdb没有了

    在命令行输入python manage.py createsuperuser按照提示输入即可记得先初始化表. django>1.7 python manage.py makemigrations ...

  5. Python安装mysql-python错误提示python setup.py egg_info

    做python项目,需要用到mysql,一般用python-mysql,安装时遇到错误提示如下: Command "python setup.py egg_info" failed ...

  6. python __init__.py用途

    转自http://www.cnpythoner.com/post/2.html Python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时, ...

  7. python setup.py uninstall

    I have installed a python package with python setup.py install How do I uninstall it? ============== ...

  8. python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案

    python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案 importerror:no mo ...

  9. Python Web.py

    安装Web.py root@bt:~# sudo pip install web.py Downloading/unpacking web.py Downloading web.py-0.37.tar ...

随机推荐

  1. Linux服务-ftp

    目录 1. ftp简介 2. ftp架构 3. ftp数据连接模式 4. 用户认证 5. vsftpd 5.1 vsftpd安装 5.2 vsftpd配置 5.3 vsftpd实验配置 5.3.1 安 ...

  2. React实战一

    目录 1. 搭建环境 2. React知识点 1. 组件 1.1 定义一个组件 1.2 组合与拆分组件 1.3 组件传值 1.4 state 1.5 生命周期函数 1.6 无状态组件 1.7 List ...

  3. string首字母大写

    定义函数将字符串首字母大写: 例1:Study hard, improve every day. def toJadenCase(string): return string.title()print ...

  4. Scrapy-从数据库取出IP并判断是否可用

    import pymysql import requests conn = pymysql.connect(host="localhost",user="root&quo ...

  5. R语言数据结构二

    上节我们讲到R语言中的基本数据类型,包括数值型,复数型,字符型,逻辑型以及对应的操作和不同数值类型之间的转换.众所周知,R语言的优势在于进行数据挖掘,大数据处理等方面,因此单个的数据并不能满足我们的需 ...

  6. plsql分页

    分页是任何一个网站(bbs,网上商城,blog)都会使用到的技术,因此学习pl/sql编程开发就一定要掌握该技术.如下图: 1.  编写无返回值的存储过程 首先是掌握最简单的存储过程,无返回值的存储过 ...

  7. [数据库]_[初级]_[sqlite3简单使用]

    场景 1.sqlite3在很多小型项目都会用到, 比如用来存储Android的联系人,短信等. 这个小巧的sqlite3是文件型的数据库, 也方便做配置文件和程序打包. 使用sql查询也方面. 所以s ...

  8. P3900 [湖南集训]图样图森破

    P3900 [湖南集训]图样图森破 链接 分析: 感觉像个暴力. 可以枚举回文串的回文中心,即枚举一个串,枚举一个串的位置作为回文中心,然后求出这个串内的回文串的长度. 此时如果回文串两端都没有到这个 ...

  9. JS获取对象“属性”的方法

    var testObj= new Object(); testObj.name = "shangguan"; testObj.age= ; testObj.action = fun ...

  10. [VB.NET][C#]二维向量的基本运算

    前言 在数学中,几何向量指具有大小(Magnitude)和方向的几何对象,它在线性代数中经由抽象化有着更一般的概念.向量在编程中也有着及其广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 基于 ...