欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

0001-20180421-自动化-python基础学习笔记

程序员文章站 2022-05-11 15:12:57
学习python 介绍: python种类: cpython(*),jpython,ironpython,rubypython,pypy ... 种类有很多,规则只有一个 python版本 Python2.7 Python3.x 学习Phtyon,学习规则 1. 安装解释器 python.exe 解 ......

======================学习python==================
介绍:
python种类:
cpython(*),jpython,ironpython,rubypython,pypy ...
种类有很多,规则只有一个

python版本
python2.7
python3.x

学习phtyon,学习规则
1. 安装解释器
python.exe 解释器程序

2. 写代码,让解释器运行
”黑框“:开始->cmd->回车

- 解释器路径 代码文件路径 (写代码)

添加操作系统环境变量:c:\python35;

python 代码文件路径
- 解释器 (简单功能测试)

3. python2.7和python3.5共存,推荐:c,d:

规则:
1. print("你好,老狗")
py2(accii): 中文报错
py3(utf-8):不报错


老外开发计算机,只能识别:
- accii码(255个东西),字母,数字,符号...
- utf-8编码,包含世间万物
代码头部写上:
#-*- encoding:utf-8 -*-

2. 变量
a、基础
变量名 = 值

print("老狗")


name = "老狗"
print(name)

ps:
变量名要求:
- 可有:字母,数字,下划线
- 数字不能开头
- 不能是python内部关键字:[ 'and','as' ...]

- 建议:见名知意

b、小高级
age = 18
age1 = age
age2 = age1
print(age2)


3.初识数据类型
age = 18 整数类型
name = "名锐杰" 字符串类型
gender = true/false 布尔类型


#比较运算
result = 19 > 18
result = 19 < 18
result 19 >= 18
result 19 == 18

#数值运算
result = 1 + 1
result = 1 - 1
result = 1 * 1
result = 1 / 1
result = "名锐杰" + "李泉" # "名锐杰李泉"
result = "名锐杰" * 8 # "名锐杰名锐杰名锐杰名锐杰名锐杰名锐杰名锐杰名锐杰"

4. 条件语句
如果:条件,
- 成立:xxxx
- 不成立:xxxx

形式一:
if 条件:
print('真')
else:
print('假')

示例:
name = input('请输入用户名')
if name == 'alex':
msg = name + '我是傻叉'
print(msg)
else:
print(name)


简化:
if 条件:
print('真')




形式二:
if 条件:
print('真')
elif 条件:
print('真')
elif 条件:
print('真')
else:
print('假')

示例:
name = input('请输入用户名')
if name == '名锐捷':
print('好帅好帅')
elif name == '李泉':
print('好浪好浪')
elif name == '刘永希':
print('好诗好诗')
else:
print('编不下去了')

示例:
name = input('请输入用户名')
if name == '名锐捷':
print('好帅好帅')
elif name == '李泉':
print('好浪好浪')
elif name == '刘永希':
print('好诗好诗')
else:
print('编不下去了')
print('end')

变形:
示例(嵌套):
name = input('请输入用户名')
if name == '名锐捷':

age = input('请输入年龄:')
if age == "18":
print('好帅好帅')
else:
print('不帅')
elif name == '李泉':
print('好浪好浪')
elif name == '刘永希':
print('好诗好诗')
else:
print('编不下去了')
print('end')



5. 输入
name = input('请输入用户名') # 让用户输入内容,输入内容赋值给name变量
print(name)

ps: 输入的内容------永远是字符串类型

6. 循环

while语句特征:

场景一:
while 条件:
条件成立,执行此处代码(执行完毕,再回到条件处)



print(1)
if true:
print(2)
print(3)
---------不会循环

print(1)
while true:
print(2)
print(3)
--------永远打印2----死循环


print(1)
while true:
print(2)
print(22)
print(32)
print(3)


print(1)
flag = true
while flag:
print(2)
flag = false
print(3)


print(1)
start = 0
flag = true
while flag:
print(start)
flag = false
print(3)



print(1)
start = 0
flag = true
while flag:
print(start)
start = start + 1
print(3)


场景二:
while 条件:
条件成立,执行此处代码(执行完毕后,再回到条件)
else:
条件不成立,




示例:
#输出: 1-100连续数字
start = 1
flag = true
while true:
print(start)
start = start + 1
if start == 100:
flag = false

print('开始...')
start = 1
flag = true
while true:
print(start)
start = start + 1
if start == 100:
print(start)
flag = false
print('结束了...')


print('开始...')
start = 1
while start < 101:
print(start)
start = start + 1
print('结束了...')

#################################
补充两个关键字: continue,break
#################################

break----跳出当前循环的外面,执行外面的代码

print(0)
while true:
print(1)
break # 跳出当前循环
print(11)
print(2)



continue------跳,跳到true的位置,继续下一次循环(下面示例中的prin11,永远不会执行)

print(0)
while true:
print(1)
continue # 结束本次循环,进行下次循环
print(11)
print(2)



使用break实现:1-100

start = 1
while true:
print(start)
start = start + 1
if start == 100
print(start)
break


输出: 1 2 3 4 5 6 8 9 10

start = 1
while start < 11:
if start == 7:
print(' ')
else:
print(start)
start = start + 1


start = 1
while start < 11:
if start == 7:
pass
else:
print(start)
start = start + 1


start = 1
while start < 11:
if start == 7:
start = start + 1
continue
else:
print(start)
start = start + 1


while循环总结:
1. 书写规则
while 条件:
成立执行...
else:
不成立执行

2. continue,break



赠送:
1. 奇偶数
num = 19
result = num % 2
if result == 0:
print('偶数')
else:
print('奇数')

2. 1-100总和

sum = 0
start = 1
while start < 101
sum = sum + start
print(sum)
start = start + 1


练习题:
1. 打印 1-99之间所有的奇数
print('开始了... ')
num = 0
while num < 100:
num = num + 1
result = num % 2
if result == 1:
print(num)
print('end')


2. 打印1-99之间所有能被3整除的数
print('开始了...')
num = 0
while num < 100:
num = num + 1
result = num % 3
if result == 0:
print(num)
print('end')

3. 打印1-99之间所有数的总和
print('开始了...')
num = 0
sum = 0
while num < 100:
num = num + 1
sum = sum + num
print(sum)
print('end')

4. 打印1-99之间所有奇数的总和
print('开始了... ')
num = 0
sum = 0
while num < 100:
num = num + 1
result = num % 2
if result == 1:
sum = sum + num
print(sum)
print('end')


5. 1-2+3-4+5-6+7...+99 之间所有数总和
print('开始了... ')
num = 0
sum = 0
sum1 = 0
sum2 = 0
while num < 100:
num = num + 1
result = num % 2
if result == 1:
sum1 = sum1 + num
# print(sum1)
else:
sum2 = sum2 + num
# print(sum2)
sum = sum1 - sum2
print(sum)
print('end')



>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>


>>> dir(__builtins__)
['arithmeticerror', 'assertionerror', 'attributeerror', 'baseexception', 'blockingioerror', 'brokenpipeerror', 'buffererror', 'byteswarning', 'childprocesserror', 'connectionabortederror', 'connectionerror', 'connectionrefusederror', 'connectionreseterror', 'deprecationwarning', 'eoferror', 'ellipsis', 'environmenterror', 'exception', 'false', 'fileexistserror', 'filenotfounderror', 'floatingpointerror', 'futurewarning', 'generatorexit', 'ioerror', 'importerror', 'importwarning', 'indentationerror', 'indexerror', 'interruptederror', 'isadirectoryerror', 'keyerror', 'keyboardinterrupt', 'lookuperror', 'memoryerror', 'nameerror', 'none', 'notadirectoryerror', 'notimplemented', 'notimplementederror', 'oserror', 'overflowerror', 'pendingdeprecationwarning', 'permissionerror', 'processlookuperror', 'recursionerror', 'referenceerror', 'resourcewarning', 'runtimeerror', 'runtimewarning', 'stopasynciteration', 'stopiteration', 'syntaxerror', 'syntaxwarning', 'systemerror', 'systemexit', 'taberror', 'timeouterror', 'true', 'typeerror', 'unboundlocalerror', 'unicodedecodeerror', 'unicodeencodeerror', 'unicodeerror', 'unicodetranslateerror', 'unicodewarning', 'userwarning', 'valueerror', 'warning', 'windowserror', 'zerodivisionerror', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
>>>

通过help查看bif的相关帮助
>>> help(input)
help on built-in function input in module builtins:

input(prompt=none, /)
read a string from standard input. the trailing newline is stripped.

the prompt string, if given, is printed to standard output without a
trailing newline before reading input.

if the user hits eof (*nix: ctrl-d, windows: ctrl-z+return), raise eoferror.
on *nix systems, readline is used if available.

>>>