Python基础学习

#!/usr/bin/env python

 Linux系统中特有,声明Python文件解释器,需要加在文件头部,编码也需要加前面,否则不生效。

1 # -*- coding:utf8 -*-
2 #coding=utf-8
3 #coding:utf-8
4 """
5 定义编码类型,主要在python2中需要声明
6 在python3中不起作用
7 """
  • Unicode:万国码,至少用16位  
  • Utf8: 能用多少位字节表示就用多少位字节,能够节省内存或硬盘空间,中文占3字节。
  • GBK:中文占2字节

python2中只要出现中文,必须加编码。

单行注释用:#

多行注释用三重引号: """ ...."""

变量名定义规则:

  • 只能由字母、数字、下划线组成
  • 不能用数字开头
  • 不能用python的关键字进行命名
  • 最好不要跟Python内置的东西重复,如内置的方法名、类名等
  • 尽量让变量名有意义

条件语句 

1 if  条件 :
2     代码块
3 else:
4     代码块
  • 一般采用4个空格进行缩进。
1 if  条件 :
2     代码块
3 elif 条件:
4     代码块
5 elif 条件:
6     代码块
7 else:
8     代码块
  • 只要有一个条件符合,后续的条件语句的代码不再执行。
  • if语句支持嵌套。

pass在if语句中的使用:

1 """
2 如果if的条件成立,但是不想执行任何操作,可以用pass进行代替,意思是什么都不执行
3 """
4 if 1 == 1:
5     pass
6 else:
7     代码块

 字符串

 1 """
 2 字符串由引号构成,可以由双引号、单引号、三个双引号、三个单引号成对构成
 3 """
 4 >>> n1 = 'Alex'
 5 >>> n2 = "Alex"
 6 >>> n3 = '''Alex'''
 7 >>> n4 = """Alex"""
 8 >>> if n1 == n2:
 9 ...     print("They are equal!")
10 ... 
11 They are equal!
12 >>> if n1 == n3:
13 ...     print("They are equal!")
14 ... 
15 They are equal!
16 >>> if n1 == n4:
17 ...     print("They are equal!")
18 ... 
19 They are equal!
20 #字符串支持加法运算
21 >>> n1+n2
22 'AlexAlex'
23 #字符串可以与数字进行乘法运算
24 >>> n1*10
25 'AlexAlexAlexAlexAlexAlexAlexAlexAlexAlex'

数字

可以进行加减乘除次方取余

 1 >>> a=39
 2 >>> b=4
 3 #
 4 >>> a/b
 5 9.75
 6 #取商
 7 >>> a//b
 8 9
 9 #取余
10 >>> a%b
11 3
12 #加法
13 >>> a+b
14 43
15 #减法
16 >>> a-b
17 35
18 #次方
19 >>> a**b
20 2313441
21 >>> b**4
22 256

循环

                                                                                                                                                                                                                                                                                                                                           

 1 #while循环
 2 while 条件成立:
 3     代码块
  else:
    代码块 #代码只执行一次
4 5 >>> count = 0 6 >>> while count <10 : 7 ... print(count,'ok',time.time()) 8 ... count+=1 9 ... 10 0 ok 1522831950.9636626 11 1 ok 1522831950.9637644 12 2 ok 1522831950.9638066 13 3 ok 1522831950.9638405 14 4 ok 1522831950.963874 15 5 ok 1522831950.9639084 16 6 ok 1522831950.9639404 17 7 ok 1522831950.963971 18 8 ok 1522831950.9640033 19 9 ok 1522831950.9640346

While循环的break和continue用法:

# continue 和 break 用法
>>> i = 1
>>> while i < 10:
...     i += 1
...     if i%2 > 0:  #非偶数时跳出输出,while循环内的后续语句不再执行,进入下个循环判断
...             continue
...     print(i)
... 
2
4
6
8
10
>>> i = 1
>>> while 1:
...     print(i)
...     i += 1
...     if i > 10:  #当大于10时跳出循环
...             break
... 
1
2
3
4
5
6
7
8
9
10

 

While循环练习题:

  • 使用while循环输入1 2 3 4 5 6  8 9 10
 1 >>> count = 1
 2 >>> while count < 11:
 3 ...     if count != 7:
 4 ...             print(count)
 5 ...     count += 1
 6 ... 
 7 1
 8 2
 9 3
10 4
11 5
12 6
13 8
14 9
15 10
  • 求1-100内的所有数的和

 

1 >>> sum = 0
2 >>> count = 1
3 >>> while count < 100:
4 ...     sum = sum + count
5 ...     count = count + 1
6 ... 
7 >>> sum
8 4950
  • 输出1-100内的所有奇数
1 >>> count = 1
2 >>> while count < 100:
3 ...     if count % 2 == 1:
4 ...             print(count)
5 ...     count += 1
6 ... 
  • 输出1-100内的所有偶数
1 >>> count = 1
2 >>> while count < 100:
3 ...     if count % 2 != 1:
4 ...             print(count)
5 ...     count += 1
6 ... ``
  • 求1-2+3-4+5...99的所有数的和
 1 >>> count = 0
 2 >>> result=0
 3 >>> while count < 100:
 4 ...     if count%2 == 1:
 5 ...             result = result + count
 6 ...     else:
 7 ...             result -= count 
 8 ...     count += 1
 9 ... 
10 >>> result
11 50
  • 用户登录(三次机会)
 1 #coding=utf-8
 2 count = 1 
 3 while count < 4:
 4     name = input("请输入用户名:")
 5     password = input("请输入密码:")
 6     if name == "root" and password == "root":
 7         print("用户名和密码正确,登录成功!")
 8         break
 9     elif count != 3:
10         print("用户名或密码错误,登录失败,请重新输入用户名和密码!")
11         count += 1
12     else:
13         print("失败%d次,请稍后再来!" %count)
14         break

 

posted @ 2018-04-04 17:37  忙碌的捕快  阅读(128)  评论(0)    收藏  举报
levels of contents