python快速学习笔记(一)-----------------字符串、数值类型、元组、列表、字典、集合

版权声明:本文为博主原创文章,转载请标明出处 https://blog.csdn.net/C2681595858/article/details/82630376

在系统的学习了C++之后开始快速学习python

一、字符串

  • 字符串换行输出:
print("""hello
world
hhahha""")

print("hello\nworld\nhahaha\n")
  • 串联字符串

    • 使用‘+’号将两个字符串串起来print("hello"+"world")
    • 将两个字符串分别打印出来print("hello","world")

    二、python中的数值

    • 有三种数值类型:整形、浮点型和虚数
    • 使用type()辨别数值类型
>>> type(1)

<class 'int'>
>>> type(1.0)

<class 'float'>
  • 可以使用shell编辑python文件且编辑器会按照语法渲染代码,让代码看起来更加美观。
  • 在字符串中包含不同的数字:(%s, %f, %d, %e)
    强制类型转换:
    注意‘%’的转义必须使用‘%’而不能使用‘\’。
    规格化输出。????????
    大数的输出。
>>> "an integer convert to a float with %%f:%f" % 10

'an integer convert to a float with %f:10.000000'

>>> "a really large number %e" % 1.604E10

'a really large number 1.604000e+10'

基本算术

  • 除的时候会自动将结果转换为浮点型
  • 取余运算%
  • 打印计算结果
>>> print("%f"%(5/3))

1.666667
  • 将数字格式化为8进制和16进制。
    • 8进制:%o,
    • 16进制:%x,

三、变量

  • 只打印一个变量的值的时候,只需要在print()中加上变量的名称。

python的另外4种基本类型:元组、列表、集合和字典。

1、元组(tuple)

    被圆括号包围。元组一旦创建,只能访问不可修改。

创建和访问方式一:

>>> filter = ("string","filled","by a","tuple")
>>> print(filter)
('string', 'filled', 'by a', 'tuple')
>>> print("A %s %s %s %s"%filter)

A string filled by a tuple

访问方式二:

>>> print("%s"%filter[0])
string
>>> print("%s"%filter[1])
filled
>>> 

其他与元组相关的函数:

  • 获取元组包含元素的数目
>>> print("%d"%len(filter))
4
>>> 
  • 也可以创建嵌套元组:
    类似于二维数组的访问
>>> print("%s"%a[0][1])
filled
>>> 

元组问题易错点

  • 若创建只包含一个元素的元组,需要在元素后面添加一个逗号,否则会被认为是一个字符串。
>>> single = ("single",)
>>> single[0]
'single'
>>> print("%s"%single[0])
single
>>> unsingle = ("unsingle")
SyntaxError: invalid character in identifier
>>> 
  • 不可对元组中的元素进行赋值操作。

2、列表

  • 列表用方括号创建
  • 列表可以随机访问,也可以修改里面元素的值。并且可以在末尾添加元素。
>>> breakfast = ["mike","egg","noodles","bread"]
>>> breakfast[1]  = "water"
>>> print("%s"%breakfast[1])
water
>>> breakfast.append("meet")
>>> print("%d"%len(breakfast))
5
>>> print(breakfast)
['mike', 'water', 'noodles', 'bread', 'meet']
>>> 
  • 一次性添加多个元素
>>> breakfast.extend(["addd1","add2","add3"])
>>> print(breakfast)
['mike', 'water', 'noodles', 'bread', 'meet', 'addd1', 'add2', 'add3']
>>> 

3、字典–以名称索引的分组数据

  • 创建空字典,逐个添加元素
>>> dictionary = {}
>>> dictionary["name"] = "chengwen"
>>> dictionary["name"]
'chengwen'
>>> 
  • 一次性创建字典
>>> diction = {"haha":123,"name":"chengwen"}
>>> diction["haha"]
123
>>> 
  • 从字典中获取键
>>> dic_key = diction.keys()
>>> print(list(dic_key))
['haha', 'name']
>>> 
  • 从字典中获取所有值
>>> dic_values = diction.values()
>>> print(list(dic_values))
[123, 'chengwen']
>>> 
  • 另一种获取字典中值的方法
>>> diction.get("haha")
123
>>> 
  • 可以像处理列表一样处理字符串。
序列的其他共有属性
  • 引用最后一个元素
>>> filter[-1]
'tuple'
>>> 
序列的范围
  • 将元组或者和列表用相同的方式进行分片
>>> tuple = ("ele1","ele2","ele3","ele4")
>>> tuple_slice = tuple[2:4]
>>> print(tuple_slice)
('ele3', 'ele4')
>>> 
  • 使用pop(index)弹出列表对应位置的元素。如果括号中没有指定弹出哪个位置的元素,将弹出最后一个位置的元素。

4、集合

可变集合和不可变集合。

  • 可以将列表作为集合的输入,从而达到去重的目的,即集合中的元素是不可重复的。
>>> alphabet = ["aaa","bbb","ccc","ddd","ddd","aaa"]
>>> aset = set(alphabet)
>>> print(aset)
{'ccc', 'aaa', 'bbb', 'ddd'}

猜你喜欢

转载自blog.csdn.net/C2681595858/article/details/82630376