python

---恢复内容开始---

1、Python的创始人:吉多·范罗苏姆(龟叔)

2、Python的分类。

      编译型:一次性,将全部的程序编译成二进制文件,然后在运行。

           优点:运行速度快。

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

      解释型:当你的程序运行时,一行一行的解释,并运行。

       优点:调式代码很方便,开发效率高,并且可以跨平台。

   缺点:运行速度慢

3、python是一门动态解释性的强类型定义语言。

     

4、 Python的分类。
            编译型:一次性,将全部的程序编译成二进制文件,然后在运行。
                优点:运行速度快。
                缺点:开发效率低,不能跨平台。       

           解释型:当你的程序运行时,一行一行的解释,并运行。
               优点:调试代码很方便,开发效率高,并且可以跨平台。
               缺点:运行速度慢。

5、变量

            ————将一些运算中间结果存在内存,以便后续程序使用。

         特点:1、数字,字母,下划线的任意结合。

                    2、不能以数字开头。

       3、不能是Python中的关键字。

       4、可描述性。  eg:不使用拼音,中文,或太长。

                                        使用下划线  #下划线                                  #驼峰体
                                                                        age_of_oldboy = 56                AgeOfOldboy = 56
                                                                       number_of_students = 80        NumberOfStudents = 80
 6、常量。

  python中常量全部大写表示             eg:BIR_OF_CHINA= 1949

 

7、  字符串(str)  只要加了“  ”、‘ ’,即为字符串。

   ps: int(str)字符串转化成数字,str全部是数字组成的。

            两者可以互转 eg:str(1111)

  bool:python中布尔值使用常量True和False来表示(首字母需要大写),不需要加引号

  msg = '''
    字符串
    '''%()

8、用户交互input。input 全是字符串

name = input('请输入你的名字:')
age = int(input('请输入你的年龄'))
msg = '''
-------info of %s------
name:%s
age:%d
-------end-------------
'''%(name,name,age)
print(msg)

 

    ps :  python2与python3对于input的使法

           python2  :  raw_input
            python3  :   input

9、字符串的拼接   

      +    *

10、占位符:%    s   str    d    digit

msg = '我叫%s,今年%d,我学习进度为10%%'%('莉莉',22)
print(msg)

 

11、if 的语句

  1.  if 条件:(:即为条件完结)

         结果

  2.if 条件: 满足条件执行代码

     else: if条件不满足就走这段

  3.while 条件:
        结果       
  如何跳出while循环:一、改变条件,跳出循环。

                二、break用于完全结束一个循环,跳出循环体执行循环后面的语句

                三、continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的 循环,break则完全终止循环

    eg:一、改变条件

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

二、break

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

输出

C:\Python36\python.exe F:/pylx/day2.py
1
2
3
4

eg:

print(111)
while True:
    print('li')
    print('shi')
    break
    print('tian')

输出

C:\Python36\python.exe F:/pylx/day2.py
111
li
shi

 

三、continue

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

输出

loop 1
loop 2
loop 3
loop 4
loop 5
loop 95
loop 96
loop 97
loop 98
loop 99
loop 100
loop 101
===out of while loop===

while ... else ..

与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句

while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

count = 0
while count <= 5 :
    count += 1
    print("Loop",count)

else:
    print("循环正常执行完啦")
print("-----out of while loop ------")

输出

Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环正常执行完啦
-----out of while loop ------

 

 

编码:

python2解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),而python3对内容进行编码的默认为utf-8。

00000001        8位bit == 1个字节(byte)

1byte    1024byte (字节)== 1kb

1kb     1024kb == 1MB

1MB      1024MB == 1GB

1GB       1024GB == 1TB  

eg.

请写出'李杰'分别用utf-8和gbk编码所占的位数

UTF-8:一个中文字符占3个字节

gbk:一个中文字符占2个字节

(一个字节等于8:0101  0101)

李杰:UTF-8=6个字节=48位

李杰:gbk=4个字节=32位

Ascll :最开始,简单的密码本,八位,最左边为0(最左边为0:第八位是设计人员为了以后发展而扩展。

ASCII码无法将世界上的各种文字和符号全部表示,所以,就需要新出一种可以代表所有字符和符号的编码,即:Unicode

Unicode(万国码):目前2个版本

两个字节(16位)表示一个字符

四个字节(32位)表示一个字符

ascii码   8位bit == 一个字节(byte)

 

1024字节 == 1kb
 
utf-8 是 unicode(万国码) 的升级版
 
gbk国产版本,为国内设计。2个字节表示一个字符

utf-8:2个中文=6个字节=48bit

gbk:2个中文=4个字节=32bi
 
简写
count = count + 1            count += 1
count = count - 1             count -= 1
sum = 2
count = 0
count += sum
print(count)

运算符

//    取整       !=   不等于

比较运算

    优先级:() > not > and > or

    同等优先级条件下从左至右以此计算。

print(4 > 3 or 4 < 3 and 1 !=1 ) 为True
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) 为T
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)         F
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)      F
print(not 2 > 1   and  3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)   F                                             

 

#x or y 如果 x为真,则值为x,否则为y
print
(4 or 3) #4 print(2 or 3) #2 print(1 or 3) #1 print(0 or 3) #3 print(-1 or 3) #-1
#x and y 如果 x 为真,则值为 y,否则为 x
and
print(2 and 3)  #3
print(1 and 3)  #3  
print(0 and 3)  #0
print(-1 and 3)  #3

print(3 or 3 > 2)  #3
print(3 > 2 or 3)  #ture
#3 > 2为Ture ——x - ture
 
 

  int与bool值之间的转换。

   1,bool 值转化成数字

a = int (True)     #1
b = int (False)    #0
print(a,b)
#Ture : 1   False: 0
a = bool(44444)  #Ture
b = bool(6.6)   #Ture
c = bool(0)     #False
d = bool(-1)    #True
print(a,b,c,d)
 

in,not in : 判断子元素是否在原字符串(字典,列表,集合)中:

 

li = 'shi'
print('s' in li)  #T
print('si' in li)  #F
print('ss' in li)  #F
print('sh' not in li)#F
print(1 and 'li' in li)#F

 

 

eg:

     print(1 or 1 > 4) # 1  (从左到右1为True就结束了)

    print(1 > 1 or 3) # 3
   print(3 > 1 or 3 or 3 > 4 and 5) # True

  # x or y 如果 x为真,则值为x,否则为y

print(3 or 0) # 3
print(0 or 3) # 3

  # x and y 如果 x 为真,则值为 y,否则为 x
print(3 and 0) # 0
print(0 and 3) # 0

 

 

posted @ 2017-10-19 16:02  小杜要加油  阅读(237)  评论(0编辑  收藏  举报