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

2018-09-18 06:45:35来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

======================学习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编码,包含世间万物
代码头部写上(-python pytho官方推荐方法):
#-*- 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)

python3 输入方法:
name = input('>>>')
python2 输入方法:
name = raw_input('>>>')

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')

"""
print('开始了... ')
num = 0
while num <= 99:
num = num + 1
result = num % 2
if result == 1:
print(num)
print('end')
"""
"""
print('开始了... ')
num = 0
while num <= 99:
result = num % 2
if result == 1:
print(num)
num = num + 1
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')


print('开始了... ')
num = 1
while num <= 99:
result = num % 3
if result == 0:
print(num)
num = num + 1
print('end')



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

print('开始了...')
num = 0
sum = 0
while num < 100:
sum = sum + num
num = num + 1
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')


print('开始了... ')
num = 1
sum = 0
while num <=99:
result = num % 2
if result == 1:
sum = sum + num
num = num + 1
print(sum)
print('end')

5. 1-2+3-4+5-6+7...+99 之间所有数总和
print('开始了... ')
num = 0
sum = 0
sum1 = 0
sum2 = 0
while num < 99:
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')

print('开始了... ')
num = 1
sum = 0
while num <=99:
result = num % 2
if result == 1:
sum = sum + num
else:
sum = sum - num
num = num + 1
print(sum)
print('end')

 

print('开始了... ')
num = 1
sum = 0
while num <=99:
result = num % 2
if result == 1:
sum = sum + num
else:
sum = sum - num
print(sum)
num = num + 1
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.

>>>

 while 循环脚本:

1 sum = 0
2 start = 1
3 while start < 999999999:
4     sum = sum + start
5     print(sum)
6     start = start + 1

 

1-99所有数求和:

 1 """
 2 print('开始了...')
 3 num = 1
 4 sum = 0
 5 while num <= 99:
 6     sum = sum + num
 7     num = num + 1
 8     print(sum)
 9 print('end')
10 """
11 
12 print('开始了...')
13 num = 0
14 sum = 0
15 while num < 100:
16     sum = sum + num
17     num = num + 1
18     print(sum)
19 print('end')

 

1-99所有奇数求和:

 1 """
 2 print('开始了... ')
 3 num = 1
 4 sum = 0
 5 while num <=99:
 6     result = num % 2
 7     if result == 1:
 8         sum = sum + num
 9     num = num + 1
10 print(sum)
11 print('end')
12 
13 """
14 print('开始了... ')
15 num = 0
16 sum = 0
17 while num < 100:
18     num = num + 1
19     result = num % 2
20     if result == 1:
21         sum = sum + num
22         print(sum)
23 print('end')

 

打印1-99所有奇数:

 1 print('开始了... ')
 2 num = 0
 3 while num <= 99:
 4     result = num % 2
 5     if result == 1:
 6         print(num)
 7     num = num + 1
 8 print('end')
 9 
10 
11 """
12 print('开始了... ')
13 num = 0
14 while num <= 99:
15     num = num + 1
16     result = num % 2
17     if result == 1:
18         print(num)
19 print('end')
20 """
21 """
22 print('开始了... ')
23 num = 0
24 while num < 100:
25     num = num + 1
26     result = num % 2
27     if result == 1:
28         print(num)
29 print('end')
30 """

 

if嵌套示例:

 1 name = input('请输入用户名:')
 2 if name == '名锐杰':
 3     age = input('请输入年龄:')
 4     if age == "18":
 5         print('好帅好帅')
 6     else:
 7         print('不帅')
 8 elif name == '李泉':
 9         print('好浪好浪')
10 elif name == '刘永希':
11         print('好诗好诗')
12 else:
13     print('编不下去了')
14 print('end')

 

模拟银行卡3次输错密码强制退出:

 1 count = 1
 2 while True:
 3     name = input('请输入用户名:')
 4     if name == "Alex":
 5         print('欢迎登陆!')
 6         num = input('请输入取款金额:')
 7     else:
 8         print('用户名输入错')
 9         count = count + 1
10         if count == 4:
11             break
12 
13 
14 
15 """
16 count = 1
17 while count < 4:
18     name = input('请输入用户名:')
19     if name == "Alex":
20         print('欢迎登陆!')
21         num = input('请输入取款金额:')
22     else:
23         print('用户名输入错')
24         count = count + 1
25 """

 

字符串运算:

 1 print("你好,老狗")
 2 
 3 name = "老狗"
 4 print(name)
 5 
 6 age = 18
 7 age1 = age
 8 age2 = age1
 9 print(age2)
10 
11 
12 result = 1 + 1
13 print(result)
14 result = 1 - 1
15 print(result)
16 result = 1 * 1
17 print(result)
18 result = 1 / 1
19 print(result)
20 result = "名锐杰" + "李泉"
21 print(result)
22 result = "名锐杰" * 8
23 print(result)
24 
25 
26 """"
27 if 条件:
28     print('真')
29 else:
30     print('假')
31 """
32 
33 name = input ('请输入用户名')
34 if name == 'alex':
35     msg = name + '是傻叉'
36     print(msg)
37 else:
38     print(name)
39 
40 """
41 简化:
42 if 条件:
43 print('真')
44 """
45 
46 """
47 形式二:    
48 if 条件:
49     print('真')
50 elif 条件:
51     print('真')
52 elif 条件:
53     print('真')
54 else:
55     print('假')
56 """
57 
58 name = input('请输入用户名')
59 if name == '名锐杰':
60     print('好帅好帅')
61 elif name == '李泉':
62     print('好浪好浪')
63 elif name == '刘永希':
64     print('好诗好诗')
65 else:
66     print('遍布下去了')
67 print('end')
68 
69 
70 
71 name = input('请输入用户名')
72 print(name)

 

打印1-99中能被3整除的数字:

 1 print('开始了... ')
 2 num = 1
 3 while num <= 99:
 4     result = num % 3
 5     if result == 0:
 6         print(num)
 7     num = num + 1
 8 print('end')
 9 
10 """
11 print('开始了...')
12 num = 0
13 while num < 100:
14     num = num + 1
15     result = num % 3
16     if result == 0:
17         print(num)
18 print('end')
19 """

 

1-99-sum-substract

 1 print('开始了... ')
 2 num = 0
 3 sum = 0
 4 sum1 = 0
 5 sum2 = 0
 6 while num < 99:
 7     num = num + 1
 8     result = num % 2
 9     if result == 1:
10         sum1 = sum1 + num
11         print(sum1)
12     else:
13         sum2 = sum2 + num
14         print(sum2)
15 sum = sum1 - sum2
16 print(sum)
17 print('end')

 

1-99-sum-odd-num-subtract-sum-even-num

 1 print('开始了... ')
 2 num = 1
 3 sum = 0
 4 while num <=99:
 5     result = num % 2
 6     if result == 1:
 7         sum = sum + num
 8     else:
 9         sum = sum - num
10         print(sum)
11     num = num + 1
12 print(sum)
13 print('end')
14 
15 """
16 print('开始了... ')
17 num = 0
18 sum = 0
19 sum1 = 0
20 sum2 = 0
21 while num < 99:
22     num = num + 1
23     result = num % 2
24     if result == 1:
25         sum1 = sum1 + num
26 #        print(sum1)
27     else:
28         sum2 = sum2 + num
29 #        print(sum2)
30 sum = sum1 - sum2
31 print(sum)
32 print('end')
33 """

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python中get pass用法

下一篇:django操作多数据库