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

  1. 《Python编程第4版 下》高清PDF|百度网盘免费下载|Python基础编程

    <Python编程第4版 下>高清PDF|百度网盘免费下载|Python基础编程 提取码:tz5v 当掌握Python的基础知识后,你要如何使用Python?Python编程(第四版)为这 ...

  2. 《Python编程第4版 上》高清PDF|百度网盘免费下载|Python基础编程

    <Python编程第4版 上>高清PDF|百度网盘免费下载|Python基础编程 提取码:8qbi  当掌握Python的基础知识后,你要如何使用Python?Python编程(第四版)为 ...

  3. 《Python游戏编程快速上手》|百度网盘免费下载|Python基础编程

    <Python游戏编程快速上手>|百度网盘免费下载| 提取码:luy6 Python是一种高级程序设计语言,因其简洁.易读及可扩展性日渐成为程序设计领域备受推崇的语言. 本书通过编写一个个 ...

  4. python基础编程——类和实例

    在了解类和实例之前,需要先了解什么是面向对象,什么又是面向过程.面向过程是以过程为中心实现一步步操作(相互调用,类似流水线思想):面向对象是以事物为中心,某个事物可以拥有自己的多个行为,而另一个事物也 ...

  5. Python基础编程:字符编码、数据类型、列表

    目录: python简介 字符编码介绍 数据类型 一.Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心 ...

  6. python第一章 python基础编程

    第一次学习python 首先python对于我来说是我学习的第三门语言,之前大一学习过了c和c++这两门语言. 接触一个新语言,首先应该的是搭载一下编译的环境.我们是老师给我们上传了的python3安 ...

  7. Python基础编程——数据类型

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 在程序设计和编程中,会涉及到各种各样的数据类型,而不同的数据类型变量之间可以进行的运算是不同的.在p ...

  8. Python 基础编程

    Python 打印九九乘法表: for i in range(1,10): for j in range(1,i+1): print j,'*',i,'=',j*i,' ', print '\n' P ...

  9. python基础编程:生成器、迭代器、time模块、序列化模块、反序列化模块、日志模块

    目录: 生成器 迭代器 模块 time 序列化 反序列化 日志 一.生成器 列表生成式: a = [1,2,3,3,4,5,6,7,8,9,10] a = [i+1 for i in a ] prin ...

随机推荐

  1. Java jdbc 连接oracle之二(使用properties文件)

    driver = oracle.jdbc.driver.OracleDriver url = jdbc:oracle:thin:@192.168.10.105:1521:orcl user = LF ...

  2. List转换成XML

    protected void Button1_Click(object sender, EventArgs e) { var customerlist = CustomerHelper.GetList ...

  3. 九十分钟极速入门Linux——Linux Guide for Developments 学习笔记

    系统信息:CentOS 64位. 一张图了解命令提示符和命令行 一些实用小命令 mkdir(make directory,创建目录).ls(list,列出当前目录下的内容).rm(remove,删除文 ...

  4. html中input标签的tabindex属性

    当浏览者浏览网站时可以通过按TAB键在网页的链接中依次移动,这是一个相当方便实用的功能.但如果网页中链接太多,恐怕按TAB键就没什么作用了,这时不妨通过改变TAB键移动的顺序来突出重点,在某些重要页面 ...

  5. c 头文件<ctype.h>(二)

    测试<ctype.h>函数 #include <stdio.h> #include <ctype.h> int main(){ ; ; i < ; ++i){ ...

  6. Java里List取并集方法retainAll不能用来判断是否有重复数据!

    网上找的源码 public boolean retainAll(Collection<?> c){     boolean modified = false;     Iterator&l ...

  7. rc.local文件

    rc.local用于自启动一些服务. 查看有哪些自启动服务: cat /etc/rc.local

  8. 浅谈UI设计中妙用无穷的深色系背景

    英文:medium 译者:优设网 - 陈子木 链接:http://www.uisdc.com/ui-benefits-of-dark-background# --------------------- ...

  9. html5与html4的区别,如何一眼区分

    还是在面试过程中遇到的这个问题,但是当时感觉回答的不是很全面,回来以后自己做的总结: 以上这个问题如果教大家看源码,可能一眼很直观的就能认出,那个是html4,那个是html5,但是面试的时候问道这个 ...

  10. Struts2相关面试题

    Struts2面试题 1.struts2工作流程 Struts 2框架本身大致可以分为3个部分: 核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心 ...