• Python基础笔记持续记录


    1.print函数,打印信息到运行界面

    print("hello world")

    2.转义字符,当我们希望字符不被Python解析,只表达字面意思,可以在符号前加\

    当\加一些特殊的字母字符时,也有另外的功能。

    1. print("he said \"Let\'s go\"")
    2. print("please print \nnextline")# \n表示换行

    3.三引号跨行字符"""

    因为Python是一行一行执行的,如果想要一次性打印出多行数据,除了用大量的print+\n的方法外,可以使用"""

    1. print("""
    2. 行行无别语
    3. 只道早还乡
    4. """)
    5. #python 会将被三引号字符包裹的文字视为一个整体

    4.引入外部库

    1. import math
    2. # 利用math求一元二次方程组,math库内的常用函数见官方文档
    3. print(math.sin(1))
    4. print(max(4, 5))
    5. a = 1
    6. b = 4
    7. c = 4
    8. print(((-b) + math.sqrt(b * b - 4 * a * c)) / 2)

    5.input()函数,获取输入数据

    用input函数获取的所有数据均为String类型,在后续使用时需要强制类型转换。

    input函数的返回值需要用变量命名,不然后续无法继续使用。

    1. #根据用户身高体重计算BMI
    2. user_weight = input("please write down your weight")
    3. user_height = input("please write down your height")
    4. print("your BMI is "+str(int(user_weight)/(float(user_height)*float(user_height))))

    6.type()函数查看对象类型

    1. print(type(None))
    2. # 返回结果

    7.字符串按索引下标取值

    1. # 可以将字符串按索引取值
    2. print("hello"[0])

    8.if_else语句

    1. if 结果为布尔类型的变量或表达式:
    2. 执行语句1
    3. 执行语句2
    4. else
    5. 执行语句3
    6. 执行语句4
    7. # 记得要写:

    9.if_elif

    等同于Java内的if_elseif

    1. user_weight = float(input("please write down your weight"))
    2. user_height = float(input("please write down your height"))
    3. user_bmi=user_weight/ user_height**2
    4. print("your BMI is " + str(user_bmi))
    5. if user_bmi<=18.5:
    6. print("体重偏瘦")
    7. elif (user_bmi > 18.5) & (user_bmi <= 25):
    8. print("体重正常")

    10.逻辑符号

    只有三个 and or not(与或非)优先级次序为not>and>or

    11.列表list

    向列表添加元素append

    删除列表内元素remove

    与其它语言不同,列表内元素类型可以不同

    列表内元素类型可以不相同,可以为布尔(注意python内的True和False都需要首字母大写,与Java不同)、浮点数、整数、嵌套list

    1. shopping_list = ["电源", 33]
    2. shopping_list.append(1.68)
    3. x=True
    4. shopping_list.append(x)
    5. print(shopping_list)
    6. shopping_list.remove("电源")
    7. #结果为 [ 33, 1.68, True]

    列表常用函数

    1. score = [33, 1.68, 6., 55, 185]
    2. print(max(score))#求最大值
    3. print(min(score))#求最小值
    4. print(sorted(score))#从小到大排序
    5. #需注意在使用比较函数时,列表内元素类型要一样
    6. # 输出为185
    7. #1.68
    8. #[1.68, 6.0, 33, 55, 185]

    12.字典

    用{}包裹,内里元素组成为key-value,其中key为不可变类型(比如list就不可以)

    1. telephone = {"mumu": 1111111, "guagua": 222222, "xiaoming": 333333}
    2. print(telephone)
    3. # 根据key来获取value
    4. print(telephone["mumu"])
    5. # 向字典内添加值,如果key已存在则不变
    6. telephone["chrome"] = 4444444
    7. print(telephone)
    8. # pop与del均为删除,pop() 函数有返回值,返回值为对应的值,del语句没有返回值
    9. print(telephone.pop("xiaoming"))

  • 相关阅读:
    C# WebBrowser无法跳转默认浏览器问题
    Java开发中如何配合MySQL实现读写分离?
    【English】语法之句子种类(陈述句、疑问句、祈使句、感叹句)
    尚硅谷设计模式(五)原型模式
    一、利用固件库模板点灯(附模板及案例程序)
    龙蜥anolis8.9安装hadoop3.3.6伪分布环境
    Mybatis——一对多关联映射
    基于Java+SpringBoot+Thymeleaf+Mysql失物招领网站平台系统设计与实现
    uniapp - 微信小程序新版本发布之后用户端如何手动更新
    LabVIEW中的数据通信方法
  • 原文地址:https://blog.csdn.net/nairuozi/article/details/126340056