zoukankan      html  css  js  c++  java
  • day1-python基础

    Hello world 程序

      执行代码如下,并保存hello.py文件,在linux下执行

    1 [oracle@dbadg ~]$ cat hello.py
    2 print("Hello world!")
    3 [oracle@dbadg ~]$ python hello.py
    4 Hello world!

      在交互器中执行

    1 C:WINDOWSsystem32>python
    2 Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
    3 Type "help", "copyright", "credits" or "license" for more information.
    4 >>> print("Hello world!")
    5 Hello world!
    6 >>>

    变量:用于存储信息

       变量声明:变量名为name,变量名name值为:“chx”

    1 name = "chx"

       变量命名规则:

    1. 变量名只能是 字母、数字或下划线的任意组合
    2. 变量名的第一个字符不能是数字
    3. 以下关键字不能声明为变量名
      ['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']

      变量的赋值

    1 name = "chx"
    2 name2 = name
    3 
    4 print (name,name2)
    5 
    6 name = "jack"
    7 print(name,name2)   #name2还会是chx,因为name2实际是通过name指向chx,而不是指向name

    注释

      单行注释:#被注释内容

    1 #name = "chx"

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

    1 1 """
    2 2 name2 = name
    3 3 
    4 4 print (name,name2)
    5 5 
    6 6 name = "jack"
    7 7 print(name,name2)
    8 8 """

    用户输入

    1 name = input("what's your name ?")
    2 print("Hello" + name)

      密码不可见

    1 #需要用到getpass模块
    2 import getpass
    3 #将值赋予pwd变量
    4 pwd = getpass.getpass("请输入密码:")
    5 #打印输出内容
    6 print(pwd )   
    pycharm上不好使,交互界面可以

    模块认识

      sys

    1 import sys
    2 print(sys.argv) 
    3 
    4 e:pythonday1>python hello.py ss          #当将参数导入使用
    5 ['hello.py', 'ss']

      os

     1 import os
     2 os.system("dir")
     3 
     4 e:pythonday1>python hello.py    #调用操作系统命令
     5  Volume in drive E is work
     6  Volume Serial Number is 8296-2703
     7 
     8  Directory of e:pythonday1
     9 
    10 03/07/2019  02:56 PM    <DIR>          .
    11 03/07/2019  02:56 PM    <DIR>          ..
    12 01/23/2019  10:13 PM                74 for.py
    13 01/23/2019  10:10 PM               540 for_age.py
    14 03/07/2019  02:56 PM               621 hello.py
    15 03/07/2019  02:32 PM               423 if.py
    16 01/23/2019  09:40 PM               422 if_age.py
    17 01/23/2019  10:15 AM               578 input.py
    18 03/05/2019  10:14 AM               178 var.py
    19 01/23/2019  09:16 PM               142 while.py
    20 03/05/2019  10:27 AM               537 while_age.py
    21                9 File(s)          3,515 bytes
    22                2 Dir(s)  22,389,821,440 bytes free

      结合使用

     1 import os,sys
     2 os.system(''.join(sys.argv[1:]))
     3 
     4 e:pythonday1>python hello.py dir     #调用系统命令
     5  Volume in drive E is work
     6  Volume Serial Number is 8296-2703
     7 
     8  Directory of e:pythonday1
     9 
    10 03/07/2019  02:52 PM    <DIR>          .
    11 03/07/2019  02:52 PM    <DIR>          ..
    12 01/23/2019  10:13 PM                74 for.py
    13 01/23/2019  10:10 PM               540 for_age.py
    14 03/07/2019  02:52 PM               559 hello.py
    15 03/07/2019  02:32 PM               423 if.py
    16 01/23/2019  09:40 PM               422 if_age.py
    17 01/23/2019  10:15 AM               578 input.py

    表达式if ... else

      场景一、用户密码登录

     1 #输入用户名密码
     2 #用户密码错误,提示用户密码错误
     3 #正确,提示欢迎,XX
     4 import getpass
     5 name = "chx"
     6 password = "123"
     7 _name = input("用户名:")
     8 #_passd = input("密码:")
     9 _passd = getpass.getpass("密码:")
    10 if _name == name and _passd == password:
    11     print("欢迎,{name} ".format(name=_name))
    12 else:
    13     print("用户名或密码错误")

      场景二、猜年龄

    程序设定年龄,让程序启动起来,用户输入后根据输入的值判断,如果猜错,提示用户值大还是小

    1 age = 24
    2 chx_age = int(input("Guess the age of chx:"))
    3 if chx_age == age :
    4     print("Yes, you guessed it")
    5 elif chx_age > age:
    6     print("No,think bigger..")
    7 else:
    8     print("No,think smaller..")

    表达式for loop

      简单循环10次

     1 for i in range(10):
     2     print("loop: ", i)
     3 #打印出来效果
     4 loop:  0
     5 loop:  1
     6 loop:  2
     7 loop:  3
     8 loop:  4
     9 loop:  5
    10 loop:  6
    11 loop:  7
    12 loop:  8
    13 loop:  9

      需求一:上面的循环,当小于5次,循环停止进入下个循环

     1 for i in range(10):
     2     if i < 5:
     3         continue
     4     print("loop: ", i)
     5 
     6 
     7 
     8 loop:  5
     9 loop:  6
    10 loop:  7
    11 loop:  8
    12 loop:  9

       需求二:还是上面的循环,当大于5次,循环停止退出

    1 for i in range(10):
    2     if i > 5:
    3         break
    4     print("loop: ",i)

     while loop  --转载金角大王博客

      死循环

    1 count = 0
    2 while True:
    3     print("你是风儿我是沙,缠缠绵绵到天涯.....")
    4     count +=1

      上面代码循环100次退出

    1 count = 0
    2 while True:
    3     print("你是风儿我是沙,缠缠绵绵到天涯.....")
    4     count +=1
    5     if count == 100:
    6         print ("去你妈的风和沙,你们这些脱了裤子是人,穿上裤子是鬼的臭男人..")
    7         break

      结合上面猜年龄,当猜错3次退出程序 while , for

     1 #while loop
     2 
     3 count = 0
     4 age = 24
     5 while count <3:
     6     chx_age = int(input("Guess the age of chx:"))
     7     if chx_age == age:
     8         print("Yes,you guessed it")
     9         break
    10     elif chx_age > age:
    11         print("No,think bigger...")
    12     else:
    13         print("No,think smaller...")
    14     count +=1
    15 
    16 #for loop
    17 
    18 count = 0
    19 age = 24
    20 for i in range(3):
    21     chx_age = int(input("Guess the age of chx: "))
    22     if chx_age == age:
    23         print("Yes,you guessed it")
    24         break
    25     elif chx_age > age:
    26         print("No,think bigger...")
    27     else:
    28         print("NO,smaller...")
    29     count +=1
  • 相关阅读:
    windows文件名非法字符过滤检测-正则表达式
    IEflash遇到flash遮挡
    打印iphone支持的所有字体
    xml字符串转xml对象,xml对象转json对象
    本地windows安装memcached服务
    resin启动时报错com.caucho.config.LineConfigException的解决
    8个Javascript小技巧,让你写的代码有腔调
    如何在MySQl数据库中给已有的数据表添加自增ID?
    mysql删除重复数据方法
    用Rem来无脑还原Web移动端自适应的页面
  • 原文地址:https://www.cnblogs.com/chhx/p/10486519.html
Copyright © 2011-2022 走看看