python基础一

 

一、计算机基础

 

  CPU是中央处理单元(Central Processing Unit)的缩写,被人们俗称为“计算机的大脑”,其负责处理、运算计算机内部的所有数据!CPU从内存或缓存中取出指令,放入指令寄存器,并对指令译码分解成一系列的微操作,然后发出各种控制命令,执行微操作系列!

  内存是计算机中重要的部件之一,它是与CPU进行沟通的桥梁。计算机中所有程序的运行都是在内存中进行的,因此内存的性能对计算机的影响非常大。特点暂时存储,供给cpu数据。 速度快成本高,断电即消失。

  内存(Memory)也被称为内存储器,其作用是用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换的数据。只要计算机在运行中,CPU就会把需要运算的数据调到内存中进行运算,当运算完成后CPU再将结果传送出来,内存的运行也决定了计算机的稳定运行。 内存是由内存芯片、电路板、金手指等部分组成的。

  硬盘是电脑主要的存储媒介之一,由一个或者多个铝制或者玻璃制的碟片组成。碟片外覆盖有铁磁性材料。相当于你电脑的数据库,存储着大量数据,文件,小电影。速度慢 ,成本低,永久保存。

  操作系统:执行者,支配所有关系。

  

二、Python的历史

  Python崇尚优美、清晰、简单,是一个优秀并广泛使用的语言。

  python2x,python3x的区别:

  •     python2x:源码混乱,重复代码较多,冗余。
  •     python3x:源码规范,崇尚优美清晰,简单。

  python发展史:

  • 1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器。Python这个名字,来自Guido所挚爱的电视剧Monty Python’s Flying Circus。他希望这个新的叫做Python的语言,能符合他的理想:创造一种C和shell之间,功能全面,易学易用,可拓展的语言。
  • 1991年,第一个Python编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。从一出生,Python已经具有了:类,函数,异常处理,包含表和词典在内的核心数据类型,以及模块为基础的拓展系统。
  • Granddaddy of Python web frameworks, Zope 1 was released in 1999
  • Python 1.0 - January 1994 增加了 lambda, map, filter and reduce.
  • Python 2.0 - October 16, 2000,加入了内存回收机制,构成了现在Python语言框架的基础
  • Python 2.4 - November 30, 2004, 同年目前最流行的WEB框架Django 诞生
  • Python 2.5 - September 19, 2006
  • Python 2.6 - October 1, 2008
  • Python 2.7 - July 3, 2010
  • In November 2014, it was announced that Python 2.7 would be supported until 2020, and reaffirmed that there would be no 2.8 release as users were expected to move to Python 3.4+ as soon as possible
  • Python 3.0 - December 3, 2008
  • Python 3.1 - June 27, 2009
  • Python 3.2 - February 20, 2011
  • Python 3.3 - September 29, 2012
  • Python 3.4 - March 16, 2014
  • Python 3.5 - September 13, 2015
  • Python 3.6 - December 16,2016

三、开发的分类及优缺点

  解释型:当程序运行时,将代码一行一行的解释成二进制,在运行。

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

    缺点:执行效率相对低。

    典型:python。

  编译型:将代码一次性全部编译成二进制,然后在运行。

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

    优点:执行效率高。

    典型:C语言。

四、python的版本

五、运行第一个python程序

    在官网下载Python编译器,在下载时注意加入环境变量选项。

    按Win+R进入运行界面输入“cmd”进入window终端,输入“python 文件路径”回车

    

1 print('我爱你,中国')
2 print(1+2+3+4)
3 print((1+2+3+4)*3 + 5)
4 print((((1+2+3+4)*3 + 5) + 100) / 3)
View Code

 

  python3x和2x的区别:

    python3x 默认编码:utf-8

    python2x 默认编码:ascii

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

六、变量

  1,变量必须由数字,字母,下划线任意组合。

  2,变量不能以数字开头。

  3,变量不能是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']

  4,变量要具有可描述性。fdjsaf = '美女'

  5,变量不能是中文。

  6,变量不能太长。

  7,官方推荐:

    #驼峰体

      AgeOfOldboy = 56

      NumberOfStudents = 100

    #下划线

      age_of_oldboy = 56

      number_of_students = 80

七、常量

    不变的量:生日,身份证号。

    python 规定没有,默认全部大写的变量 成为常量

    例如:

    BIRTH = 19970425

八、注释

  为什么有注释?

  帮助你记起之前的代码,或者帮助别人看懂你的代码。

  注释种类:

  •   单行注释:#
  •   多行注释:'''被注释内容''' """被注释内容"""

九、基础数据类型初始

  1、数字类型 int。

    用于计算。

    type() 判断此数据是什么数据类型。

    

1 # 数字类型
2 i = 100
3 print(i * 3)

 

  2、字符串类型 str

    用于字符串(加引号就是字符串)

    type() 判断此数据是什么数据类型。

 1 #字符串类型
 2 #在python中被引号引起来的数据就是字符串。
 3 name = '老男孩'
 4 name2 = '路飞学院'
 5 print(name,name2)
 6 #配合使用
 7 msg = "My name is Alex , I'm 22 years old!"
 8 name3 = '''太白'''
 9 msg = '''
10     今天我想写首小诗,
11     歌颂我的同桌,
12     你看他那乌黑的短发,
13     好像一只炸毛鸡。
14 '''
15 print(msg)
16 #字符串:+与字符串+     * 与数字乘
17 #字符串的拼接 + 
18 n1 = '老男孩'
19 n2 = '     是最好的培训机构'
20 n3 = n1 + n2
21 print(n3)
22 #字符串的相乘*
23 i = '坚强'
24 print(i*8)   

  3、BOOL值 True False

1 print(1 > 2 and 3 < 4 or 4 > 5)
2 print(1,type(1))
3 print('老男孩',type('老男孩'))
4 print(False,type(False))
5 print('False',type('False'))

 

十、用户交互

  input 数据类型全部是字符串类型

    python2x : raw_input()

    python3xinput()

1 name = input("请输入名字")
2 age = input("请输入年龄")
3 hobby = input("请输入爱好")
4 s = "我的名字是" + name + ",我的年龄" + age + ",我的爱好" + hobby
5 print("我的名字是%s,我的年龄是%s,我的爱好%s" % (name,age,hobby))
6 print(s)

 

 

 

 

 

第一种结构:

    1 if 条件:结果 

  第二种结构:

1 if 条件:
2     结果
3 else:
4     结果
if 3 > 2:
    print(666)
print(222)

  第三种结构:   

1 choice = input('请输入你的猜的数字:')
2 if choice == '2':
3     print('我请你吃饭')
4 elif choice == '6':
5     print('免一周作业')
6 elif choice == '3':
7     print('一起去大保健')

  第四种结构:

choice = input('请输入你的猜的数字:')

1 choice = input('请输入你的猜的数字:')
2 if choice == '2':
3     print('我请你吃饭')
4 elif choice == '6':
5     print('免一周作业')
6 elif choice == '3':
7     print('一起去大保健')
8 else:
9     print('选择错误.....')

  第五种结构:

1 if 条件:
2     if 条件:
3     结果
4     else:
5         结果
6 else:
7     结果    
1 age = int(input('请猜我的年龄:'))
2 if True:
3     if age <= 18:
4         print('恭喜你猜对了')
5     else:
6         print('这都看出来...')
7 else:
8     print(666) 

  int ---> str          例如:str(12)   数字,字符都可以转换

  str ---> int          例如:int(123) 可以,int(123q')不可以  全部由数字组成的字符串才能转换成数字

 

 

 

 

 

1 while 条件:
2     结果
 1 while True:
 2     print('')
 3     print('凉凉')
 4     print('体面')
 5     print('社会摇')
 6 print(222)
 7 while True:
 8     print(111)
 9     print(333)
10 print(444)

  跳出循环的条件:

    1、改变条件。
    2、break。

  break : 结束循环。
  continue:结束本次循环,继续下一次循环。

  标志位flag

1 flag = True
2 while flag:
3     print('')
4     print('凉凉')
5     print('体面')
6     print('社会摇')
7     flag = False
1 flag = True
2 count = 1
3 while flag:
4     print(count)
5     count = count + 1
6     if count == 101:
7         flag = False    
1 count = 1
2 while count < 101:
3     print(count)
4     count = count + 1
while True:
    print(111)
    print(222)
    break
    print(333)
    print(333)

count = 1
while True:
    print(count)
    count = count + 1
    if count == 101:
        break    

练习题:

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

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

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

4、输出 1-100 内的所有偶数

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

6、用户登陆(三次机会重试)

 1 #第一题
 2 count = 1
 3 while count <= 10:
 4     print(count)
 5     count += 1
 6 first = 1
 7 
 8 #第二题
 9 sum = 0
10 while first <= 100:
11     sum += first
12     first += 1
13 print(sum)
14 
15 #第三题
16 number = 1
17 while number <= 100:
18     if number%2 == 1:
19         print(number)
20     number += 1
21 
22 #第四题    
23 number = 1
24 while number <= 100:
25     if number%2 == 0:
26         print(number)
27     number += 1
28 
29 #第五题    
30 sum = 0
31 number = 1
32 while number < 100:
33     if number%2 == 1:
34         sum += number
35     else:
36         sum -= number
37     number += 1
38 print(sum)
39 
40 #第六题
41 count = 0
42 while count < 3:
43     name = input("请输入用户名")
44     password = input("请输入密码")
45     if name == ‘abc’ and password == ‘abc’:
46         print("输入正确")
47         break
48     else:
49         print("输入错误")
50         count += 1

 

posted @ 2018-03-16 20:01  小杰~~  阅读(458)  评论(0编辑  收藏  举报