一、Python基本语法结构

学习基本语法,应从官方编辑器Python Shell学起,响应速度快,方便单句执行

打开Python官方IDLE,切换至Python Shell


就是它 ↓


然后就是一顿测试操作猛如虎,各种测

Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> tax=12.5/100
>>> price=100
>>> price*tax
12.5
>>> price+_
112.5
>>> _
112.5
>>> _/_
1.0
>>> print('asdf')
asdf
>>> print("asdf")
asdf
>>> print('  "asdfasd" ')
  "asdfasd" 
>>> print(''asdfafd'')
SyntaxError: invalid syntax
>>> print('"asdfasdf"')
"asdfasdf"
>>> print("  asfd ""a\sdf")
  asfd a\sdf
>>> '字符串直接叠加'
'字符串直接叠加'
>>> "asdf" "asdfassadf"
'asdfasdfassadf'
>>> "asdf" +3*" er"
'asdf er er er'
>>>  text = ('Put several strings within parentheses '
            'to have them joined together.')
 
SyntaxError: unexpected indent
>>> text = ('Put several strings within parentheses '
            'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'
>>> text[25]
'n'
>>> text[3]
' '
>>> text[0:26]
'Put several strings within'
>>> text[:26]
'Put several strings within'
>>> text[25:]
'n parentheses to have them joined together.'
>>> text[:]
'Put several strings within parentheses to have them joined together.'
>>> len(text)
68
>>> print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

>>> print('C:\some\name')
C:\some
ame
>>> print(r'C:\some\name')
C:\some\name
>>> '复核数据类型 list (列表)'
'复核数据类型 list (列表)'
>>> tt = [1, 4, 9, 16, 25]
>>> tt
[1, 4, 9, 16, 25]
>>> tt=[1,'sdf',26.4]
>>> tt
[1, 'sdf', 26.4]
>>> tt[2]
26.4
>>> tt[0]
1
>>> tt[1:26]
['sdf', 26.4]
>>> tt[-6]
Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    tt[-6]
IndexError: list index out of range
>>> tt[-2]
'sdf'
>>> tt+['ewe',56,23.4,text[25]]
[1, 'sdf', 26.4, 'ewe', 56, 23.4, 'n']
>>> tt[0]='可修改'
>>> tt
['可修改', 'sdf', 26.4]
>>> tt.append("加一个")
>>> tt
['可修改', 'sdf', 26.4, '加一个']
>>> tt[1:3]=[1,'qwer',666]
>>> tt
['可修改', 1, 'qwer', 666, '加一个']
>>> tt[2:3]=[]
>>> tt
['可修改', 1, 666, '加一个']
>>> tt=[]
>>> tt
[]
>>> len(tt)
0
>>> a = ['a', 'b', 'c']
>>> b = [1, 2, 3]
>>> x = [a, b]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][2]
'c'


回到IDE,打开Sublime text 3,新建一个test.py文件

这时候就可以复制一些代码进入运行(Ctrl+B)一下了
# _*_coding:utf-8_*_	这行表示此文档编码方式为utf-8
# Author:AeeeSs 2018-5-5

a, b = 0, 1
while b < 10:
	print(b,end=',')
	a, b = b, a+b

print('题目:有1、2、3、4个数字 能组成多少个互不相同且无重复数字的三位数?都是多少!')
print('程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。')
print('程序源代码:')

d = []
for i in range(1,5):
	for j in range(1,5):
		for k in range(1,5):
			if(i != k) and (i != j) and (j != k):
				d.append({i,j,k})
print("总数量:", len(d))
print(d)

Python的简洁,是建立在对代码格式的严格要求上的,Tab与空格不能混用,每一行的缩进都严格要求,因为缩进是 Python 组织语句的方法,  它用缩进来表示上下行代码的从属关系的

这时候就可以复制一些代码进入运行(Ctrl+B)一下了,开启我们的Python之旅

发布了8 篇原创文章 · 获赞 2 · 访问量 1987
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览