python基础编程
1.if else
var1 = 100
if var1:
print ("1 - if 表达式条件为 true")
print (var1) #为0时,条件不成立
var2 = 0
if var2:
print ("2 - if 表达式条件为 true")
print (var2)
else:
print("2 - if条件不成立")
print ("Good bye!")
打印:
1 - if 表达式条件为 true
100
2 - if条件不成立
Good bye!
age = int(input("input your dog’s age:"));
if age < 0:
print("你逗我呢吧...");
elif age == 1:
print("相当于14岁的人");
elif age == 2:
print("相当于22岁的人");
elif age > 2:
#计算年龄
humanAge = 22 + (age - 2)*5;
print("对应人类年龄:", humanAge);
1 - if 表达式条件为 true
100
2 - if条件不成立
Good bye!
2.字符串操作
'''
Created on 2016年12月2日 @author:
'''
# 字符串操作 a = "Hello";
b = "Python"; print("a+b=", a + b);#字符串连接 HelloPython
print("a*2=", a * 2);#重复输出字符串 HelloHello
print("a[2]=", a[2]);#通过索引获取字符串中字符 l
print("a[:4]", a[:4]);#截取字符串中的一部分 Hell
print("o in a:", "o" in a);#如果字符串中包含给定的字符返回 True
print("xx not in a:", "xx" not in a);#如果字符串中不包含给定的字符返回 True
#"r"或者"R" : 原始字符串,没有转义或者特殊字符
print(r"\n");#\n
# % : 格式字符串
print("%s 是字符串, %d 是数字" % (b, 12));#Python 是字符串, 12 是数字 # 3个引号
hi = ''' hai how are you,
第二行
''';
print("3引号hi:", hi);
#Unicode 字符串
print("Unicode 字符串:" + u"Hello\u0020World !"); #====================================================== mystr = "this is string example....wow!!!";
capstr = mystr.capitalize();#首字母大写
print("原字符串:", mystr);
print("首字母大写:", capstr);
print("居中:", mystr.center(4));
print("统计【i】出现的次数:", mystr.count("i", 0 ,len(mystr)));
a+b= HelloPython
a*2= HelloHello
a[2]= l
a[:4] Hell
o in a: True
xx not in a: True
\n
Python 是字符串, 12 是数字
3引号hi: hai how are you,
第二行
Unicode 字符串:Hello World !
原字符串: this is string example....wow!!!
首字母大写: This is string example....wow!!!
居中: this is string example....wow!!!
统计【i】出现的次数: 3
3.函数
#函数定义
def printStr(mystr):
"函数功能:打印传入的字符串"
print("传入参数为:", mystr);
return; #函数调用
printStr("helloPython");
传入参数为: helloPython
#lambda函数的语法只包含一个语句,如下:lambda [arg1 [,arg2,.....argn]]:expression
sum = lambda arg1, arg2 : arg1 + arg2;
print("相加:",sum(20, 100));
print("相加2:",sum(-20, -100));
相加: 120
相加2: -120
4.for用法
#基本for循环
languages = ["C", "C++", "Perl", "Python"];
for i in languages:
print(i); #Break
print("--------------------------");
sites = ["Baidu", "Google","Runoob","Taobao"]
for s in sites:
if s == "Runoob":
print("到Runoob,循环停止");
break;
else:
print(s);
print("===end===");
C
C++
Perl
Python
--------------------------
Baidu
Google
到Runoob,循环停止
===end===
#for和数列
#如果你需要遍历数字序列,可以使用内置range()函数。它会生成数列
#0~n-1的值
for i in range(10):#0 1 2 ... 9
print(i); #区间的值
print("----------------")
for j in range(3, 11):
print(j);
0
1
2
3
4
5
6
7
8
9
----------------
3
4
5
6
7
8
9
10
5.模块调用

#_Module 新建一个模块
#模块方法1:
def print_info():
print("模块_Module.print_info()被调用了");
return; #模块方法2:加法计算
def add(a, b):
print("结果:", a + b);
return a + b;
#_ModuleCall 导入其他模块,可使用所有函数
import _Module #调用其他模块的函数
_Module.print_info();
_Module.add(3, 5);
模块_Module.print_info()被调用了
结果: 8
#_ModuleCall2 只导入其他模块的其中部分函数
from _Module import add add(10, 9);
结果: 19
6.日历
#日期 import calendar; cal_2016_1 = calendar.month(2016, 1);
print("2016年1月日历:");
print(cal_2016_1);
2016年1月日历:
January 2016
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
python基础编程的更多相关文章
- 《Python编程第4版 下》高清PDF|百度网盘免费下载|Python基础编程
<Python编程第4版 下>高清PDF|百度网盘免费下载|Python基础编程 提取码:tz5v 当掌握Python的基础知识后,你要如何使用Python?Python编程(第四版)为这 ...
- 《Python编程第4版 上》高清PDF|百度网盘免费下载|Python基础编程
<Python编程第4版 上>高清PDF|百度网盘免费下载|Python基础编程 提取码:8qbi 当掌握Python的基础知识后,你要如何使用Python?Python编程(第四版)为 ...
- 《Python游戏编程快速上手》|百度网盘免费下载|Python基础编程
<Python游戏编程快速上手>|百度网盘免费下载| 提取码:luy6 Python是一种高级程序设计语言,因其简洁.易读及可扩展性日渐成为程序设计领域备受推崇的语言. 本书通过编写一个个 ...
- python基础编程——类和实例
在了解类和实例之前,需要先了解什么是面向对象,什么又是面向过程.面向过程是以过程为中心实现一步步操作(相互调用,类似流水线思想):面向对象是以事物为中心,某个事物可以拥有自己的多个行为,而另一个事物也 ...
- Python基础编程:字符编码、数据类型、列表
目录: python简介 字符编码介绍 数据类型 一.Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心 ...
- python第一章 python基础编程
第一次学习python 首先python对于我来说是我学习的第三门语言,之前大一学习过了c和c++这两门语言. 接触一个新语言,首先应该的是搭载一下编译的环境.我们是老师给我们上传了的python3安 ...
- Python基础编程——数据类型
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 在程序设计和编程中,会涉及到各种各样的数据类型,而不同的数据类型变量之间可以进行的运算是不同的.在p ...
- Python 基础编程
Python 打印九九乘法表: for i in range(1,10): for j in range(1,i+1): print j,'*',i,'=',j*i,' ', print '\n' P ...
- python基础编程:生成器、迭代器、time模块、序列化模块、反序列化模块、日志模块
目录: 生成器 迭代器 模块 time 序列化 反序列化 日志 一.生成器 列表生成式: a = [1,2,3,3,4,5,6,7,8,9,10] a = [i+1 for i in a ] prin ...
随机推荐
- simple mail example for smtp debug
vim /etc/mail.rc head /etc/rc.local | mail -s "test_email" pyz_sub1@mailtest.com
- touches
e.touches.length//有多少个手指接触头
- Tanks坦克大战
创建工程,场景: 将素材导入,Unity5以上的版本,无需担心素材包的路径问题,中文路径也可以直接导入了,简单方法就是将素材包直接拖到Project面板 游戏所需要的场景在Prefabs里的Level ...
- phonegap android 输入法弹出会遮盖表单框的解决办法
phonegap android 当页面内容比较多,表单超出屏幕范围时,点击输入,输入法会遮盖住表单框,而且无法向上滑动. 经过测试发现,是由于config.xml中设置了 FullScreen 的全 ...
- iphone H5视频行内播放(禁止全屏播放)
一般用户都知道,ios在网页点击视频播放时,视频会弹出全屏播放框. video标签的playsinline.webkit-playsinline标记根本就不会起作用. 还有传闻说对于没有声音的视频不会 ...
- Magento-找出没有图片的产品
最近维护网站,发现网站的产品很多都没有图片显示,看了一下是因为没有在后台勾选图片,就是 image small_image thumbnail 这三项,就算有图片如果没有勾选的话也不会显示出来,产品 ...
- Mount DVD on CentOS
Mount DVD on CentOS need to mount CD/DVD on CentOS Temporarily or Permanently? Here’s the Process Us ...
- 写个shell脚本
以前更新网站程序都是手动噼里啪啦敲代码,即麻烦又慢,还神经紧张.终于忍不住写个shell脚本. cd /usr/local/tomcat7/apache-tomcat-9.0.0.M4/ bin/ ...
- drdb
Distributed Replicated Block Device(DRBD)是一种基于软件的,无共享,复制的存储解决方案,在服务器之间的对块设备(硬盘,分区,逻辑卷等)进行镜像.DRBD工作在内 ...
- Supervisor重新加载配置
Supervisor重新加载配置启动新的进程 liaojie 发布于 1年前,共有 0 条评论 一.添加好配置文件后 二.更新新的配置到supervisord supervisorctl update ...