python语法基础

1.输出:print()函数输出

hello world!

打开python的IDLE编辑器

>>>执行单行

如果执行多行的话就ctrl+n 建立文件批量运行

print"hello python!") 回车执行

 

多行就ctrl+n窗口中输入

 fn+f5执行了

2.注释: 

1> #注释法

2>三引号注释法

ctrl+O打开对应的文档

多行注释:

或者同时使用3个双引号

 3.标识符

标注某个东西的符号

命名规则:

第一个字符为字母或者下划线

除了第一个字符以外的其他字符可以是字母、数字、下划线

例如 _2w_,jwrer_

4.变量

变化的量

赋值:  abc=9  abc成为变量了因为赋值了

5.数据类型

python中常用的数据类型有:

数、字符串、列表(list)、元组(tuple)、集合(set)、字典(dictionary)、

列表和元组相当于数组

-------------------------------------------结果---------------------------------------------------------------

 6.运算符

python中常见的运算符有: + - * / %

 

7.缩进

python是一门强烈缩进的语言,同一层次的代码,处于同一个缩进幅度上,下一层次的代码,需要相对于上一层次的代码进行缩进,建议使用tab键缩进

 

 

 

===========================================附录代码===================

"""
print("HELLO PYTHON")
print("HELLO PYTHON")
print("HELLO PYTHON")
print("HELLO PYTHON")
print(".py")
"""
abc=9
'''数 字符串 列表、元组,集合、字典'''
#字符串
a='abc'
b="abc"
c='''abc'''
#列表:存储多个元素的东西
d=[7,'asc',9]
'''可以用下标来获取值'''
'''列表中的元素是可以替换的<可以重新赋值>'''
#元组:类似列表,但是区别是元组中的元素不可以重新赋值,且用小括号表示
e=(7,'yuanzu',9)
#字典:字典用{键:值,键:值...}键用标识符表示
'''取值格式: 字典名["对应键名"]'''
f={"1":a,2:'wrwe'}
g={"name":"lili","gender":"man"}
#集合
'''集合最大的好处是去重'''
h=set("abfbddwe")
'''{'b', 'e', 'd', 'w', 'a', 'f'}没有重复的字母'''
i=set('zfdf')
'''求差集{'a', 'b', 'w', 'e'}'''
#j=h-i
'''求并集'''

==================f5输出 ==============

>>> a
'abc'
>>> b
'abc'
>>> c
'abc'
>>> d
[7, 'asc', 9]
>>> d[0]
7
>>> d[3]
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
d[3]
IndexError: list index out of range
>>> d[0]=123
>>> d
[123, 'asc', 9]
>>> e
(7, 'yuanzu', 9)
>>> e[0]
7
>>> e[3]
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
e[3]
IndexError: tuple index out of range
>>> e[0]=123
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
e[0]=123
TypeError: 'tuple' object does not support item assignment
>>> f
{2: 'wrwe', '1': 'abc'}
>>> f[1]
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
f[1]
KeyError: 1
>>> f['1']
'abc'
>>> f[2]
'wrwe'
>>>  h
{'b', 'e', 'd', 'w', 'a', 'f'}
>>> j
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
j
NameError: name 'j' is not defined
>>>  h
{'e', 'a', 'b', 'd', 'f', 'w'}
>>> i
{'f', 'z', 'd'}
>>> h-i
{'a', 'b', 'w', 'e'}

 

posted on 2019-03-04 20:59  companion  阅读(187)  评论(0编辑  收藏  举报