zoukankan      html  css  js  c++  java
  • python语言入门 第一天


    初识Python

    一、python介绍

    - 解释器:
    cpython(默认使用)
    ipython(shell)
    jpython(java)
    ironpython
    rubypython

    - 编码:
    ascii:用一个字节=8比特,标识计算机能表达的所有东西
    unicode:万国码,用4个字节=32位来做对应关系
    utf-8:针对万国码进行压缩,至少使用1个字节表示
    gbk:针对亚洲国家的文字所做的对应关系
    中文2字节=16位
    注意:utf-8编码中中文占3个字节,gbk中中文占2个字节

    归纳:

    编码大小支持语言
    ASCII 1个字节 英文
    Unicode 2个字节(生僻字4个) 所有语言
    UTF-8 1-6个字节,英文字母1个字节,汉字3个字节,生僻字4-6个字节 所有语言


    - 版本:
    python2:解释器默认编码:ascii
    格式:# -*- coding:utf-8 -*-

    python3:解释器默认编码:utf-8
    格式:无需配置编码格式

    - IDE:
    - pycharm
    - 文字大小
    - 模板
    - vim
    - notepad

    二、模块:
    - time:
    time模块提供各种时间相关的功能
    与时间相关的模块有:time,datetime,calendar

    - getpass:
    加密输入密码

    - os:
    包含普遍的操作系统功能


    三、语法:

    - 输入、输出:
    print:
    print("你是疯儿他是傻")
    input:
    # 密文密码
    # import getpass
    # user = input("enter a user,please")
    # passwd = getpass.getpass("enter a num,please")
    # print(user,passwd)

    - 变量:
    格式:变量名 = 值
    规范:
    a.数字、字母、下划线
    b.不能以数字开头
    c.不能使用python关键字
    建议:简明知意;user_pwd = "xxx"
    示例:
    name = 'anthony'
    user = 'anthony'

    - 数据类型:
    age = 18 # 整数类型
    name = "anthony" #字符串类型
    列表:
    user_list = ["紫薇","尔康","18","海量","小鸡"]
    n3 = user_list[0]
    n4 = user_list[1] # "尔康"

    user_list = ["紫薇","尔康","18","海量","小鸡"]

    for xxx in user_list:
    print(xxx)
    if xxx == '18':
    break
    字典:
    user_info = {"name":"紫薇","age":18}


    n5 = user_info["name"]
    n6 = user_info["age"]

    user_info['count'] = 666
    # {"name":"紫薇","age":18,"count":666}

    数据类型嵌套:

    n7 = ["alex","eric",[11,22,33]]

    n7[1]
    n7[2][1]

    n8 = [
    "alex",
    {'name':'日天','age':18},
    [11,22,33]
    ]
    n8[1]["age"] = 19


    - 条件语句:
    格式:
    格式一:
    if 条件:
    成功之后走这里

    格式二:
    if 条件:
    成功之后走这里
    else:
    失败之后走这里

    格式三:
    if 条件:
    成功之后走这里
    elif 条件:
    成功之后走这里
    elif 条件:
    成功之后走这里
    else:
    上述都失败
    示例:

    # while True:
    # # 定义类目
    # msg = '''
    # 欢迎来到大世界
    # 1、可乐
    # 2、雪碧
    # 3、柠檬茶
    # 4、退出
    # '''
    # # 打印类目
    # print(msg)
    # # 定义选项
    # choice = input("请输入你选择的类目:")
    #
    # # 判断所选类目
    # if choice == '1':
    # print("1、伏特加 2、葡萄酒 3、白酒4、退出")
    # # 定义酒品类目
    # search_type = input("请输入你选择的酒品类目:")
    # if search_type == '1':
    # print("伏特加")
    # elif search_type == '2':
    # print("葡萄酒")
    # elif search_type == '3':
    # print("白酒")
    # else:
    # print("输入错误")
    # break
    # elif choice == '2':
    # print("雪碧")
    # elif choice == '3':
    # print("柠檬茶")
    # elif choice == '4':
    # break
    # else:
    # print("输入错误")

    - 循环语句:
    格式:
    while 条件:
    条件成立执行

    while True:
    print('钓鱼要钓刀鱼,刀鱼要到岛上钓')

    while 1==1 and 2==2:
    print('钓鱼要钓刀鱼,刀鱼要到岛上钓')

    timer = 0
    while timer < 3:
    print('钓鱼要钓刀鱼,刀鱼要到岛上钓')
    timer = timer + 1

    print('完成')

    示例:
    # count = 1
    # while count < 11:
    # if count == 7:
    # count +=1
    # continue
    # print(count)
    # count +=1

    - 循环退出:
    - break:
    定义:强制终止当前所在循环
    示例:
    示例一:
    while True:
    print('钓鱼要钓刀鱼,刀鱼要到岛上钓')
    break
    示例二:
    页面上输出 1 - 10

    a. count = 1
    while count < 11:
    print(count)
    count = count + 1

    b. count = 1
    while True:
    print(count)
    count = count + 1
    if count == 11:
    break

    c. count = 1
    while True:
    print(count)
    if count == 10:
    break
    count = count + 1
    - continue:
    定义:跳出本次循环,继续下一次循环。
    示例:
    示例一:
    a. count = 1
    while count < 11:
    if count == 7:
    count = count + 1
    continue
    print(count)
    count = count + 1


    b. count = 1
    while count < 11:
    if count == 7:
    pass
    else:
    print(count)
    count = count + 1

    - 循环练习:
    a. 1-100的所有数的和
    # 定义基数
    sum = 0
    count = 1

    # 循环开始
    while True:
    if count == 100:
    break
    else:
    count += 1
    sum += count
    print(sum)

    b. 1-2+3-4+5 ... 99的所有数的和
    sum = 0
    for count in range(1,101):
    if count % 2 == 1:
    sum -= count
    elif count % 2 == 0:
    sum += count
    print(sum)



  • 相关阅读:
    gc buffer busy/gcs log flush sync与log file sync
    给Oracle年轻的初学者的几点建议
    Android 编程下帧动画在 Activity 启动时自动运行的几种方式
    Android 编程下 Touch 事件的分发和消费机制
    Java 编程下 static 关键字
    Java 编程下 final 关键字
    Android 编程下模拟 HOME 键效果
    Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated ?
    Extjs4 大型项目目录结构重构
    [转]SQLServer 2008 允许远程连接的配置方法
  • 原文地址:https://www.cnblogs.com/ipyanthony/p/9009972.html
Copyright © 2011-2022 走看看