13个 Python 必备的知识,建议收藏!(上)

上一篇 / 下一篇  2022-07-21 13:37:59

  Python在编程语言流行指数PYPL中已多次排名第一。
  由于其代码可读性和更简单的语法,它被认为是有史以来最简单的语言。
  NumPy、Pandas、TensorFlow等各种AI和机器学习库的丰富性,是Python核心需求之一。
  如果你是数据科学家或 AI/机器学习的初学者,那么Python是开始你的旅程的正确选择。
  本次,小F会带着大家探索一些Python编程的基础知识,虽然简单但都很有用。
  ·目录
  · 数据类型
  · 变量
  · 列表
  · 集合
  · 字典
  · 注释
  · 基本功能
  · 条件语句
  · 循环语句
  · 函数
  · 异常处理
  · 字符串操作
  · 正则表达式
  1、数据类型
  数据类型是可以存储在变量中的数据规范。解释器根据变量的类型为变量分配内存。
  下面是Python中的各种数据类型。
  2、变量
  变量是存放数据值的容器。
  变量可以使用短名称(如x和y)或更具描述性的名称(age、carname、total_volume)。  
  Python 变量命名规则:
  · 变量名必须以字母或下划线字符开头
  · 变量名称不能以数字开头
  · 变量名只能包含字母数字字符和下划线(A-z、0-9和_)
  · 变量名称区分大小写(age、Age和AGE是三个不同的变量)
  var1 = 'Hello World'
  var2 = 16
  _unuseful = 'Single use variables'
  输出结果如下。
  3、列表
  列表(List)是一种有序和可更改的集合,允许重复的成员。
  它可能不是同质的,我们可以创建一个包含不同数据类型(如整数、字符串和对象)的列表。?
  >>> companies = ["apple","google","tcs","accenture"]
  >>> print(companies)
  ['apple', 'google', 'tcs', 'accenture']
  >>> companies.append("infosys")
  >>> print(companies)
  ['apple', 'google', 'tcs', 'accenture', 'infosys']
  >>> print(len(companies))
  5
  >>> print(companies[2])
  tcs
  >>> print(companies[-2])
  accenture
  >>> print(companies[1:])
  ['google', 'tcs', 'accenture', 'infosys']
  >>> print(companies[:1])
  ['apple']
  >>> print(companies[1:3])  
  ['google', 'tcs']
  >>> companies.remove("infosys")
  >>> print(companies)
  ["apple","google","tcs","accenture"]
  >>> companies.pop()
  >>> print(companies)
  ["apple","google","tcs"]
  4、集合
  集合(Set)是一个无序和无索引的集合,没有重复的成员。
  对于从列表中删除重复条目非常有用。它还支持各种数学运算,例如并集、交集和差分。
  >>> set1 = {1,2,3,7,8,9,3,8,1}
  >>> print(set1)
  {1, 2, 3, 7, 8, 9}
  >>> set1.add(5)
  >>> set1.remove(9)
  >>> print(set1)
  {1, 2, 3, 5, 7, 8}
  >>> set2 = {1,2,6,4,2}  
  >>> print(set2)
  {1, 2, 4, 6}
  >>> print(set1.union(set2))        # set1 | set2
  {1, 2, 3, 4, 5, 6, 7, 8}
  >>> print(set1.intersection(set2)) # set1 & set2
  {1, 2}
  >>> print(set1.difference(set2))   # set1 - set2
  {8, 3, 5, 7}
  >>> print(set2.difference(set1))   # set2 - set1
  {4, 6}
  5、字典
  字典是作为键值对的可变无序项集合。
  与其他数据类型不同,它以【键:值】对格式保存数据,而不是存储单个数据。此功能使其成为映射JSON响应的最佳数据结构。
  >>> # example 1
  >>> user = { 'username': 'Fan', 'age': 20, 'mail_id': 'codemaker2022@qq.com', 'phone': '18650886088' }
  >>> print(user)
  {'mail_id': 'codemaker2022@qq.com', 'age': 20, 'username': 'Fan', 'phone': '18650886088'}
  >>> print(user['age'])
  20
  >>> for key in user.keys():
  >>>     print(key)
  mail_id
  age
  username
  phone
  >>> for value in user.values():
  >>>  print(value)
  codemaker2022@qq.com
  20
  Fan
  18650886088
  >>> for item in user.items():
  >>>  print(item)
  ('mail_id', 'codemaker2022@qq.com')
  ('age', 20)
  ('username', 'Fan')
  ('phone', '18650886088')
  >>> # example 2
  >>> user = {
  >>>     'username': "Fan",
  >>>     'social_media': [
  >>>         {
  >>>             'name': "Linkedin",
  >>>             'url': "https://www.linkedin.com/in/codemaker2022"
  >>>         },
  >>>         {
  >>>             'name': "Github",
  >>>             'url': "https://github.com/codemaker2022"
  >>>         },
  >>>         {
  >>>             'name': "QQ",
  >>>             'url': "https://codemaker2022.qq.com"
  >>>         }
  >>>     ],
  >>>     'contact': [
  >>>         {
  >>>             'mail': [
  >>>                     "mail.Fan@sina.com",
  >>>                     "codemaker2022@qq.com"
  >>>                 ],
  >>>             'phone': "18650886088"
  >>>         }
  >>>     ]
  >>> }
  >>> print(user)
  {'username': 'Fan', 'social_media': [{'url': 'https://www.linkedin.com/in/codemaker2022', 'name': 'Linkedin'}, {'url': 'https://github.com/codemaker2022', 'name': 'Github'}, {'url': 'https://codemaker2022.qq.com', 'name': 'QQ'}], 'contact': [{'phone': '18650886088', 'mail': ['mail.Fan@sina.com', 'codemaker2022@qq.com']}]}
  >>> print(user['social_media'][0]['url'])
  https://www.linkedin.com/in/codemaker2022
  >>> print(user['contact'])  
  [{'phone': '18650886088', 'mail': ['mail.Fan@sina.com', 'codemaker2022@qq.com']}]
  6、注释
  单行注释,以井字符(#)开头,后面带有消息并在行尾结束。
  # 定义用户年龄
  age = 27
  dob = '16/12/1994' # 定义用户生日
  多行注释,用特殊引号(""")括起来,你可以将消息放在多行中。
  """
  Python小常识
  This is a multi line comment
  """
  7、基本功能
  print()函数在控制台中打印提供的消息。此外你还可以提供文件或缓冲区输入作为在屏幕上打印的参数。
  print(object(s), sep=separator, end=end, file=file, flush=flush)
  print("Hello World")               # prints Hello World  
  print("Hello", "World")            # prints Hello World?
  x = ("AA", "BB", "CC")
  print(x)                           # prints ('AA', 'BB', 'CC')
  print("Hello", "World", sep="---") # prints Hello---World
  input()函数用于收集来自控制台的用户输入 。
  这里需要注意,input()会把你输入的任何内容转换为字符串。
  因此,如果你将年龄作为整数值提供,但input()方法将其作为字符串返回,此时就需要手动将其转换为整数。
  >>> name = input("Enter your name: ")
  Enter your name: Codemaker
  >>> print("Hello", name)
  Hello Codemaker
  len()可以查看对象的长度。如果你输入一个字符串,则可以获取指定字符串中的字符数。
  >>> str1 = "Hello World"
  >>> print("The length of the string  is ", len(str1))
  The length of the string  is 11
  str()用于将其他数据类型转换为字符串值。
  >>> str(123)
  123
  >>> str(3.14)
  3.14
  int()用于将字符串转换为整数。
  >>> int("123")
  123
  >>> int(3.14)
  3

TAG: 软件开发 Python

 

评分:0

我来说两句

Open Toolbar