python基础

1.扩展名相关

 一般情况下python可以执行.py以外的其他扩展名的文件

导入模块时,如果不是.py结尾的文件,会出错

所以python文件后缀名需以.py结尾

2.执行方式

python 解释器+py文件名→执行文件

进入python解释器→实时输入并获取执行结果

3.解释器路径

#!/usr/bin/env python:在linux下以./*.py运行文件时,文件头部必须加上#!/usr/bin/env python,且对*.py文件要有可执行权限

4.编码

#-*- coding:utf8 -*-:告诉python解释器用什么编码去编

ascii 码     8位

unicode     至少16位

utf8           能用多少表示就用多少表示,中文用3个字节表示

gbk            专门用来编码中文的,中文用2个字节表示

1字节=8位

python3无需关注头部,python2文件中只要出现中文,头部必须加

5.用户名密码输入程序

1 n1 = input('请输入用户名:')
2 n2 = input('请输入密码:')
3 if n1 == "root" and n2 == "root!23":
4     print("登录成功")
5 else:
6     print("登录失败")

a.input表示永远等待,直到用户输入了值,就会将输入的值赋值给n,n代指用户输入的内容,也叫做变量(代指某一个变化的值)

   Input接收的都是字符串类型

b.变量只能由字母、数字、下划线组成,不能以数字开头,python 内置的不要用(sum...),python关键字也不能使用('and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except','exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield')

c.当行注释:# 被注释内容

   多行注释:""" 被注释内容 """

d.条件语句

1.if基本语句

1 if 2 == 2:
2     print('欢迎进入一中')
3     print('欢迎进入二中')
4 else:
5     print('欢迎进入四中')

2.if语句嵌套:语句执行顺序

3.if elif

 1 inp = input('请输入会员级别:')
 2 
 3 if inp == '高级':
 4     print('美女')
 5 elif inp == '白金':
 6     print('大摩')
 7 elif inp == '铂金':
 8     print('明星')
 9 else:
10     print('城管')
11     
12 print('开始服务')

4.补充

缩进用四个空格,一个缩进是一个代码块

=表示赋值

==表示比较

pass指代空代码,无意义,仅仅用于表示代码块

1 if 1 == 1:
2     pass
3 else:
4     print('111')

6.基本数据类型

字符串(引号引起来的'内容'   "内容"  '''内容'''  """内容""",不可以单双引号混搭):可以做加法和乘法(让字符串重复出现n次),不可以做减法和除法

数字:没有引号引起来,并且是数字,可以做加减乘除 (+-*/)

**代表次方

%代表取余

//代表取商

7.while循环

a.死循环:line 6永远不会执行

1 import time
2 
3 while 1 == 1:
4     print('OK', time.time())
5     
6 print(123)

b.构造动态条件

1 count = 0
2 
3 while count < 10:
4     print(count)
5     count = count + 1
6     
7 print(123)

c.while else

1 count = 1
2 while count < 10:
3     print(count)
4     count = count + 1
5 else:
6     print('else')

d.continue break

1 count = 0
2 while count < 10:
3     count = count + 1
4     print(count)
5     continue
6     print(123)
7 print('end')

遇到continue不再执行下一句,而是直接返回循环(终止当前循序,开始下一次循环)

break表示终止所有循环

1 count = 0
2 while count < 10:
3     count = count + 1
4     print(count)
5     break
6     print(123)
7 print('end')

e.练习

1、使用while循环输入 1 2 3 4 5 6     8 9 10

1 n = 1
2 while n < 11:
3     if n == 7:
4         pass
5     else:
6         print(n)
7     n = n + 1

2、求1-100的所有数的和

1 n = 1
2 s = 0
3 while n < 101:
4     s = s + n
5     n = n + 1
6 print(s)

3、输出 1-100 内的所有奇数

1 n = 1
2 while n < 101:
3     tmp = n % 2
4     if tmp == 1:
5         print(n)
6     else:
7         pass
8     n = n + 1

4、求1-2+3-4+5 ... 99的所有数的和

 1 n = 1
 2 s = 0
 3 while n < 100:
 4     tmp = n % 2
 5     if n == 0:
 6         s = s - n
 7     else:
 8         s = s + n
 9     n = n + 1
10 print(s)

5.用户登陆(三次机会重试,错误大于等于3次,要求退出)

 1 count = 1
 2 while count < 4:
 3     user =  input('用户名:')
 4     pwd =  input('密码:')
 5     if user == 'root' and pwd == '123456':
 6         print('登录成功')
 7         break
 8     else:
 9         print('用户名或者密码错误')
10     count =  count + 1
posted on 2018-04-10 21:17  吃猫粮的狗  阅读(146)  评论(0编辑  收藏  举报