Python基本语法--数据结构与运算符
# -*- coding: utf-8 -*-
print "Hello, Python!";
print ("Hello, Python!"); #行和缩进
if True:
print "True"
else:
print "False" if True:
print "Answer"
print "True"
else:
print "Answer"
# 没有严格缩进,在执行时保持
print "False" #多行语句
item_one=1
item_two=2
item_three=3
total = item_one + \
item_two + \
item_three
print total days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
days #引号
word = 'word'
sentence = "这是一个句子。"
paragraph = """这是一个段落。
包含了多个语句"""
print word
print sentence
print paragraph #注释
# 第一个注释
print "Hello, Python!"; # 第二个注释
name = "Madisetti" # 这是一个注释 '''
这是多行注释,使用单引号。
这是多行注释,使用单引号。
这是多行注释,使用单引号。
''' """
这是多行注释,使用双引号。
这是多行注释,使用双引号。
这是多行注释,使用双引号。
""" #空行
raw_input("\n\nPress the enter key to exit.") import sys; x = 'foo'; sys.stdout.write(x + '\n') #码组
'''
if expression :
suite
elif expression :
suite
else :
suite
''' #帮助
help(sys.stdout.write) #变量赋值
counter = 100 # 赋值整型变量
miles = 1000.0 # 浮点型
name = "John" # 字符串 print counter
print miles
print name a = b = c = 1
print a,b,c
a, b, c = 1, 2, "john"
print a,b,c #数字
var1 = 1
var2 = 10 #del var1[,var2[,var3[....,varN]]]]
var=5896419821
var_a=0.22
var_b=3e2
del var
del var_a, var_b #字符串
#s="a1a2•••an"(n>=0)
s = 'ilovepython'
print s[1:5]
print s[5:-1] str = 'Hello World!'
print str # 输出完整字符串
print str[0] # 输出字符串中的第一个字符
print str[2:5] # 输出字符串中第三个至第五个之间的字符串
print str[2:] # 输出从第三个字符开始的字符串
print str * 2 # 输出字符串两次
print str + "TEST" # 输出连接的字符串 #列表
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john'] print list # 输出完整列表
print list[0] # 输出列表的第一个元素
print list[1:3] # 输出第二个至第三个的元素
print list[2:] # 输出从第三个开始至列表末尾的所有元素
print tinylist * 2 # 输出列表两次
print list + tinylist # 打印组合的列表 #元组
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john') print tuple # 输出完整元组
print tuple[0] # 输出元组的第一个元素
print tuple[1:3] # 输出第二个至第三个的元素
print tuple[2:] # 输出从第三个开始至列表末尾的所有元素
print tinytuple * 2 # 输出元组两次
print tuple + tinytuple # 打印组合的元组 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # 元组中是非法应用
list[2] = 1000 # 列表中是合法应用 #元字典
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'} print dict['one'] # 输出键为'one' 的值
print dict[2] # 输出键为 2 的值
print tinydict # 输出完整的字典
print tinydict.keys() # 输出所有键
print tinydict.values() # 输出所有值 #算术运算符
a = 21
b = 10
c = 0 c = a + b
print "Line 1 - Value of c is ", c c = a - b
print "Line 2 - Value of c is ", c c = a * b
print "Line 3 - Value of c is ", c c = a / b
print "Line 4 - Value of c is ", c c = a % b
print "Line 5 - Value of c is ", c a = 2
b = 3
c = a**b
print "Line 6 - Value of c is ", c a = 10
b = 5
c = a//b
print "Line 7 - Value of c is ", c #比较运算符
a = 21
b = 10
c = 0 if ( a == b ):
print "Line 1 - a is equal to b"
else:
print "Line 1 - a is not equal to b" if ( a != b ):
print "Line 2 - a is not equal to b"
else:
print "Line 2 - a is equal to b" if ( a <> b ):
print "Line 3 - a is not equal to b"
else:
print "Line 3 - a is equal to b" if ( a < b ):
print "Line 4 - a is less than b"
else:
print "Line 4 - a is not less than b" if ( a > b ):
print "Line 5 - a is greater than b"
else:
print "Line 5 - a is not greater than b" a = 5;
b = 20;
if ( a <= b ):
print "Line 6 - a is either less than or equal to b"
else:
print "Line 6 - a is neither less than nor equal to b" if ( b >= a ):
print "Line 7 - b is either greater than or equal to b"
else:
print "Line 7 - b is neither greater than nor equal to b" #赋值运算符
a = 21
b = 10
c = 0 c = a + b
print "Line 1 - Value of c is ", c c += a
print "Line 2 - Value of c is ", c c *= a
print "Line 3 - Value of c is ", c c /= a
print "Line 4 - Value of c is ", c c = 2
c %= a
print "Line 5 - Value of c is ", c c **= a
print "Line 6 - Value of c is ", c c //= a
print "Line 7 - Value of c is ", c #位运算符
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0 c = a & b; # 12 = 0000 1100
print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001
print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011
print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000
print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111
print "Line 6 - Value of c is ", c #逻辑运算符
a = 10
b = 20
c = 0 if ( a and b ):
print "Line 1 - a and b are true"
else:
print "Line 1 - Either a is not true or b is not true" if ( a or b ):
print "Line 2 - Either a is true or b is true or both are true"
else:
print "Line 2 - Neither a is true nor b is true" a = 0
if ( a and b ):
print "Line 3 - a and b are true"
else:
print "Line 3 - Either a is not true or b is not true" if ( a or b ):
print "Line 4 - Either a is true or b is true or both are true"
else:
print "Line 4 - Neither a is true nor b is true" if not( a and b ):
print "Line 5 - Either a is not true or b is not true or both are not true"
else:
print "Line 5 - a and b are true" #成员运算符
a = 10
b = 20
list = [1, 2, 3, 4, 5 ]; if ( a in list ):
print "Line 1 - a is available in the given list"
else:
print "Line 1 - a is not available in the given list" if ( b not in list ):
print "Line 2 - b is not available in the given list"
else:
print "Line 2 - b is available in the given list" a = 2
if ( a in list ):
print "Line 3 - a is available in the given list"
else:
print "Line 3 - a is not available in the given list" #身份运算符
a = 20
b = 20 if ( a is b ):
print "Line 1 - a and b have same identity"
else:
print "Line 1 - a and b do not have same identity" if ( id(a) == id(b) ):
print "Line 2 - a and b have same identity"
else:
print "Line 2 - a and b do not have same identity" b = 30
if ( a is b ):
print "Line 3 - a and b have same identity"
else:
print "Line 3 - a and b do not have same identity" if ( a is not b ):
print "Line 4 - a and b do not have same identity"
else:
print "Line 4 - a and b have same identity" #运算符优先级
a = 20
b = 10
c = 15
d = 5
e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ", e e = ((a + b) * c) / d # (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ", e e = (a + b) * (c / d); # (30) * (15/5)
print "Value of (a + b) * (c / d) is ", e e = a + (b * c) / d; # 20 + (150/5)
print "Value of a + (b * c) / d is ", e
Python基本语法--数据结构与运算符的更多相关文章
- Python初学者第三天 运算符、while循环
3day Python基础语法 1.运算符:算数运算符.比较运算符.赋值运算符.逻辑运算符 A.算数运算符:a=10,b=3 + 加 a+b - 减 a-b * 乘 a*b / 除 a/b ...
- python基础语法、数据结构、字符编码、文件处理 练习题
考试范围 '''1.python入门:编程语言相关概念2.python基础语法:变量.运算符.流程控制3.数据结构:数字.字符串.列表.元组.字典.集合4.字符编码5.文件处理''' 考试内容 1.简 ...
- Python 基本语法,文件读写,数据结构和类型
Python 基本语法,文件读写,数据结构和类型 1.基本语法 解释型(无需编译).交互式.面向对象.跨平台.简单好用 中文编码:http://www.cnblogs.com/huxi/archive ...
- python学习第三讲,python基础语法之注释,算数运算符,变量.
目录 python学习第三讲,python基础语法之注释,算数运算符,变量. 一丶python中的基础语法,注释,算数运算符,变量 1.python中的注释 2.python中的运算符. 3.pyth ...
- python基础语法(运算符及优先级)
python基础语法(运算符及优先级) python语言支持的运算符类型 算数运算符 假设变量a为10,变量b为21 算数符 描述 实例 + 加-两个对象相加 a+b结果31 - 减-得到一个负数或者 ...
- Python 基本语法1
Python 基础语法(一) Python的特点 1. 简单 Python是一种代表简单思想的语言. 2. 易学 Python有极其简单的语法. 3. 免费.开源 Python是FLOSS(自由/开放 ...
- python基础语法及知识点总结
本文转载于星过无痕的博客http://www.cnblogs.com/linxiangpeng/p/6403991.html 在此表达对原创作者的感激之情,多谢星过无痕的分享!谢谢! Python学习 ...
- python基础语法(一)
Python的特点 1. 简单 Python是一种代表简单思想的语言. 2. 易学 Python有极其简单的语法. 3. 免费.开源 Python是FLOSS(自由/开放源码软件)之一. 4. 高层语 ...
- Python基础语法(转)
作者:Peter 出处:http://www.cnblogs.com/Peter-Zhang/ Python 基础语法(一) Python的特点 1. 简单 Python是一种代表简单思想的语言. ...
随机推荐
- ABC: Always Be Coding
ABC: Always Be Coding (原地址:https://medium.com/@davidbyttow/abc-always-be-coding-d5f8051afce2) Be h ...
- vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)
使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ...
- window.onload 和 $(document).ready(function(){})的区别
这篇作为我的新的起点开始吧,发现年纪大了,记性就不好了,有些东西老是记了忘,忘了百度.在学一些新知识的时候也是这样的毛病,总是重复学习,这样效率真心差!所以决定开始认真写博客! 本来想封装一个预加载的 ...
- 使用Python对Access读写操作
学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件. 1.导入模块 import win ...
- 在ASP.NET Core中使用Apworks快速开发数据服务
不少关注我博客的朋友都知道我在2009年左右开发过一个名为Apworks的企业级应用程序开发框架,旨在为分布式企业系统软件开发提供面向领域驱动(DDD)的框架级别的解决方案,并对多种系统架构风格提供支 ...
- Struts2之i18N国际化
对于i18n其实没有太多内容,一般的公司用不到这些内容,除非是跨国公司,但即便是跨国公司也不一定会使用i18n来进行国际化处理,所以本篇内容仅供大家了解,不做深入的探讨,希望通过本篇内容,可以帮助大家 ...
- 使用$.post和action或servlet交互 URL出现 http://localhost:8080/../[object%20Object] 错误的问题解决
使用$.post时,如下所示: $.post({ url : "./test/ajaxTest", }); 控制台报:There is no Action mapped for n ...
- Learning to Rank简介
Learning to Rank是采用机器学习算法,通过训练模型来解决排序问题,在Information Retrieval,Natural Language Processing,Data Mini ...
- 一个web应用的诞生(11)--列表分页
上章的结束,若在实际开发过程中,会发现一个问题,那就首页或关注分享,是一下子按时间顺序全部显示出来,这在实际项目中不可能出现的,想想实际中的产品是如何做的? 一般来说,无非是两种,一种是使用页码,来进 ...
- Linux服务器性能查看分析调优
一 linux服务器性能查看 1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc ...