python学习笔记1-基础语法
2024-09-07 03:44:22
1 在3版本中print需要加上括号
2 多行语句:用\连接
item_one=1
item_two=2
item_three=3
total = item_one + \
item_two + \
item_three
print (total)
3 引号
字符串通常在引号中 不管是单引号 双引号还是三引号
必须保证前后一致
#引号
word = 'word'
sentence = "这是一个句子。"
paragraph = """这是一个段落。
包含了多个语句"""
print (word)
print (sentence)
print (paragraph)
4注释:
(1)#开头 也可以在结尾用#进行注释
(2)多行注释 用三引号括起来
#注释
# 第一个注释
print ("Hello, Python!"); # 第二个注释
name = "Madisetti" # 这是一个注释 '''
这是多行注释,使用单引号。
这是多行注释,使用单引号。
这是多行注释,使用单引号。
''' """
这是多行注释,使用双引号。
这是多行注释,使用双引号。
这是多行注释,使用双引号。
"""
5码组:
#码组
'''
if expression :
suite
elif expression :
suite
else :
suite
'''
6帮助
help(sys.stdout.write)
7变量赋值
#变量赋值
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
8数据类型
Numbers
(1)不可以改变的数据类型
(2)用于存储数值
(3)int long float complex
string
(1)str*2代表输出2次
(2)可以用+进行字符串的连接
#字符串
#s="a1a2•••an"(n>=0)
s = 'ilovepython'
print (s[1:5])#从0开始
print (s[5:-1])#去不到末尾的n str = 'Hello World!'
print (str) # 输出完整字符串
print (str[0]) # 输出字符串中的第一个字符
print (str[2:5]) # 输出字符串中第三个至第五个之间的字符串
print (str[2:]) # 输出从第三个字符开始的字符串
print (str * 2) # 输出字符串两次
print (str + "TEST") # 输出连接的字符串
List
(1)用中括号[]创建 用,分开
(2)*和+的用法和string类似
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
(1)用小括号()
(2)元素是固定的 相当于可读
(3)用法和上面的string list类似
(4)元祖中不能直接赋值给某个元素 tuple[2]=1000//wrong
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 # 列表中是合法应用
Dictionary
(1)用{},无序集合
(2)采用键值的方式 例如'name':'join'
(3)可以输出所有的键和值
#元字典
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()) # 输出所有值
9运算符
9.1算数运算符
(1)幂 **
(2)取整除 //
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)
9.2比较运算符
(1)== 比较对象是否相等
9.3位运算符
(1)&:位与:两个相应位都为1 结果为1
(2)|:位或:其中一个为1则为1
(2)^:位异或:相异的时候为1
(4)~:取反:0-》1 1——》0
(5)<<:左移:
(6)>>:右移
#位运算符
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
9.4成员运算符
(1)in:在指定序列中找到值返回true
(2)not in
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 lis)t"
9.5身份运算符
(1)is:判断是不是引用的同一个对象
(2)is not:判断两个标识符是不是引用不同对象
#身份运算符
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")
最新文章
- php循环删除文件目录及文件
- Filter的用法之注解
- codevs1011 数的计算 2001年NOIP全国联赛普及组
- ActionBar官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示
- dwz框架---(2)表单回调函数
- 部署wcf出现的问题与解决方法
- Chapter 1 Securing Your Server and Network(13):配置端点安全性
- 转载 jQueryEasyUI Messager基本使用
- C#多线程和线程池问题
- 第九节 java7JDK的常用封装类型
- IDEA设置注释的颜色
- react创建项目报错unexpected end of json while parsing near xxx
- 区块链扩容方案之Gas值限制
- Solidworks如何把装配体做成单个零件
- MySQL数据库Innodb储存引擎----储存页的结构
- Alpha 冲刺 —— 十分之七
- 使用redis存放 map数据
- Xshell6破解
- 巨蟒django之CRM4 一些小功能
- Unity 移动 和 旋转 [小结]
热门文章
- Building a Radio Listening Station to Decode Digital Audio &; Police Dispatches
- C++类型的转换
- jQuery -&;gt; 获取后代元素的三种方法
- Autolayout和VFL
- LeetCode_3Sum
- USB/IP项目总结
- error: expected &#39;=&#39;, &#39;,&#39;, &#39;;&#39;, &#39;asm&#39; or &#39;__attribute__&#39; before &#39;{&#39; token
- Guava Cache在实际项目中的应用
- tomcat 部署项目的多种方式
- antV G6流程图在Vue中的使用