第一节:

1.python的介绍

2.第一个程序

    print(‘hello world’)

    python XX.py

    ./XXX.py

 

3.python的变量

   #!/usr/bin/env python

或 #!/usr/local/bin/python2.7

   # -*-  conding:utf-8 -*-(python 3不需要)

   #Author:glj

   变量就是为了存储,方便调用

   定义变量 name=“ xxxxx”

   print(“my name is”,name) 相当于两个参数传给了print

 

   a1=hjhjhj

   a2=a1

   a1=dede

  print(“name:”a1,a2)

  变量名只能是字母、数字或下划线任意组合

  变量名第一个字符不能是数字

  以下关键字不能是变量名:

  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.二进制

  计算机只认识:0、1 

  1          1         1        1        1        1       1       1       

 128        64        32        16        8        4       2       1  

 

5. 字符编码

   ASCII :最多是8位表示一个字节 (8位最多表示255的字符)一个字符占8位,ASCII码最多只能表示 255 个符号。

   Unicode 每个字符占2个字节也就是16位(不管中英文,最少2个字节,可能更多

   utf-8 :可变长的(英文的还是存的ASCII,存中文是utf-8 但是占3个字节)

   python2版本默认ASCII

   python3是默认utf-8

 

6.用户交互程序

 注释: ‘''fhkd

    jdlsfd

    hdfqjw’’’ 多行打印

example1//:

username=input(‘usename:’) 

password=input(‘password:’)

print(username,password) 

name=input('name:')

password=input('password:')

age=input('age:')

msg=''' username:%s password:%s age:%s ''' %(name,password,age)

print (msg)

 

example2//:

name=input('name:')

password=input('password:')

age=int(input('age:’))

msg=''' username:%s user:%s password:%s age:%d ''' %(name,name,password,age)

print (msg)

#print(type(age))

 

example3//:

name=input('name:')

password=input('password:')

age=int(input('age:'))

msg=''’ username:{name1} password:{password1} age:{age1} '''.format(name1=name,password1=password,age1=age)

print(msg)

 

example4//:

name=input('name:')

password=input('password:')

age=int(input('age:'))

msg=''' username:{0} password:{1} age:{2} '''.format(name,password,age)

print (msg)

print(type(age))

密文:

import getpass 

password=getpass.getpass(‘password:’)    (把password=input('password:’)换掉)

7.if else

example1//:

name=‘glj'

password=12345

username=input('username:')

password=int(input('password:'))

if name==username and passwordpass==password:

print(("{na} successfully login ").format(na=username))

else:

print("failed login”)

 

example2//:

age=23 

userage=int(input('age:'))

if userage==age:

print("ok!")

elif userage<age:

print("smaller")

else:

print("bigger!”)

 

8.for

最简单的循环10次

for i in range(10):

    print("loop:", i )

输出:

loop: 0

loop: 1

loop: 2

loop: 3

loop: 4

loop: 5

loop: 6

loop: 7

loop: 8

loop: 9

需求一:还是上面的程序,但是遇到小于5的循环次数就不走了,直接跳入下一次循环

 

for i in range(10):

    if i<int(sys.argv[1]):

        continue #不往下走了,直接进入下一次loop

    print("loop:", i )

python for.py 10

 

需求二:还是上面的程序,但是遇到大于5的循环次数就不走了,直接退出

for i in range(10):

    if i>5:

        break #不往下走了,直接跳出整个loop

    print("loop:", i )

continue是跳出本次循环,进行下一次循环

break是跳出本次循环

 

9. while:

 

 count=0

while True:

  print("count:",count)

  count=count+1

 

example//:

age = 23

count = 0

while True:

    userage = int(input('age:'))

    if userage == age:

        print("ok!")

        break

    elif userage < age:

        print("smaller")

    else:

        print("bigger!")

    count = count + 1    

    if count == 3:

        print("too many times!")

        break

 

example// :

age = 23

count = 0

while count < 3:

    userage = int(input('age:'))

 

    if userage == age:

        print("ok!")

        break    

    elif userage < age:

        print("smaller")

    else:

        print("bigger!")

    count = count + 1

else:

    print("too many times!")

 

example//:

age = 23

count = 0

for i in range(3):

    userage =int(input('age:'))

    if userage == age:

       print("ok!")

       break

    elif userage < age:

       print("smaller")

    else:

       print("bigger!")

    count = count +1

else:

    print("too many times!”)

 

example//: 

age=23

count=0

while count<3:

      userage=int(input('age:'))

      if userage==age:

          print("ok!")

          break

      elif userage<age:

          print("smaller")

      else:

          print("bigger!")

      count=count+1

      if count==3:

          confirm = input("想继续猜吗?")

          if confirm !='n':

              count=0

          else :

              print("too many times!”)