自学PYTHON分享 --基础1

1.python2和python3的区别:

宏观上:python2 python3 区别:

       python2 源码不标准,混乱,重复代码太多,

       python3 统一 标准,去除重复代码。

2.python的环境

编译型:一次性将所有程序编译成二进制文件。

缺点:开发效率低,不能跨平台。

优点:运行速度快。

CC++等等。

解释型:当程序执行时,一行一行的解释。

优点:开发效率高,可以跨平台。

缺点:运行速度慢。

:python ,php,等等。

3.运行第一个py文件:

python3x :python 文件路径 回车

python2x :python2 文件路径 回车

python2 python3 区别:python2默认编码方式是ascii

  解决方式:在文件的首行:#-*- encoding:utf-8 -*-

  python3 默认编码方式utf-8

4.变量。

变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。

变量的规范:

  1. 必须由数字,字母,下划线任意组合且不能数字开头

         *r = 4 字符不行    ___ = 4 可以

2.不能是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']

3.变量具有可描述性。 /age = 18 而不是 name = 18/

#下划线

age_of_oldboy = 56

number_of_students = 80

4.不能是中文。 // 中文不会报错//

5. 最好不要太长!

1 age1 = 12 
2 age2 = age1
3 age3 = age2
4 age2 = 100
5 print(age1,age2,age3) 
View Code

  # 答案 12 100 12

7.常量。

一直不变的量。     π  // 不能用拼音写//

BIR_OF_CHINA = 1949

8.注释。

方便自己方便他人理解代码。

单行注释:#

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

 

9.用户交互。Input (交互式)

   1.等待输入,

   2.将你输入的内容赋值给了前面变量。

   3.input出来的数据类型全部是str

 

10.基础数据类型初始。

数字:int 12,3,45

    + - * / **

% 取余数

ps:type()  判断方法

       print(100,type(100))          print('100',type('100'))

       Python2中有long python3    中全是int 没有long类型

字符串转化成数字:int(str) 条件:str必须是数字组成的。

数字转化成字符串:str(int)

 

字符串:strpython当中凡是用引号引起来的都是字符串。

可相加:字符串的拼接 但是不能相减

可相乘:字符串可以数字相乘    str * int

msg = """ /'''

今天我想写首小诗,

歌颂我的同桌,

你看他那乌黑的短发,

好像一只炸毛鸡。

"""/'''

#print(msg)  可以这样

bool:布尔值。 True False 首字母大写 不加引号

 

11.If条件语句

if 空格条件:(冒号前面的是条件 后面是结果 冒号的作用就是让条件和结果分开)

缩进4个空格结果

 

不会报错 777不在if语句里面 直接输出

1.#第一种

if 4 > 5 :
    print('我请你喝酒')
print('喝什么酒')

#第二种:
if 4 > 5:
    print('我请你喝酒')
else:
    print('喝什么酒')

 不管if成不成立 都出输出 喝什么酒

 第二种是做选择 行就直接if 不行走下面

2

num = input('请输入您猜的数字:')

if num == '1':
    print('一起抽烟')
elif num == '2':
    print('一起喝酒')
elif num == '3':
    print('新开了一家,走看看')
else:
    print('你猜错了.....')

    Input出来是字符串 下面的都是要‘’ 不可以直接打1

 // 两个等于是比较 一个是赋值//

 3

score = int(input("输入分数:"))

if score > 100:
    print("我擦,最高分才100...")
elif score >= 90:
    print("A")
elif score >= 60:
    print("C")
elif score >= 80:
    print("B")
elif score >= 40:
    print("D")
else:
    print("太笨了...E")

 

  //elif 代表分支  多选 //

4

name = input('请输入名字:')
age = input('请输入年龄:')

if name == '小二':
    if age == '18':
        print(666)
    else:
        print(333)
else:
    print('错了....')

12while循环

while 条件:

 

 222不输出

循环体 ------无限循环。

终止循环2个方法1.改变条件,使其不成立。

            2.break  ---- 立马中断 跳出循环

count = 1
flag = True
#标志位
while flag:
    print(count)
    count = count + 1
    if count > 100 :
        flag = False


count = 1
while count <= 100:
    print(count)
    count = count + 1


count = 1
sum = 0

while count <= 100:
    sum = sum + count 
    count = count + 1
    
print(sum)

 

Continue-----结束本次循环  不结束整体循环

count = 0
while count <= 100 : 
    count += 1
    if count > 5 and count < 95: 
        continue 
    print("loop ", count)

print("-----out of while loop ------")
    

猜你喜欢

转载自www.cnblogs.com/wangjiadee/p/10679856.html