Python 之路之 Python 基础 (一)

文章
林里克斯

Python 之路之 Python 基础 (一)


Python Version:Python 3.9.1



1.注释

  • Ctrl + / 快捷 # 注释 (先选着需要注释的内容,然后按下快捷键 Ctrl + /)
    • eg:
#我是注释
  • ''' 3 个 单引号多行注释
    • eg:
'''
我是注释
'''
  • """ 3 个 双引号多行注释
    • eg:
"""
我是注释
"""

2.数据类型

  • 数字类型 (int)
    • int 整数类型
    • float 浮点类型
    • long 长整数型(Python3 已经废弃使用)
    • complex 复数
  • 字符串类型 (str)
    • 使用 单引号双引号 包裹起来的一段普通文字
  • 布尔类型 (bool)
    • True 正确
    • False 错误
  • 列表类型 (list)
    • eg:
list = ['我是列表1','我是列表2','我是列表3']
  • 字典类型 (dict)
    • eg:
person = {'id':1,'name':'Jarbo','age'=18}
  • 元组类型 (tuple)
    • eg:
nums = (1,8,9,2,4,5,0)
  • 集合类型 (set)
    • eg:
x = {3,'Jarbo','Python','True'}

使用 type 内置类查看变量对应的数据类型;

a = '你好世界!'
b = 123
c = True
d = ['test', 'Jarbo', 'big']
e = {'id': 1, 'name': 'Jarbo', 'age': 18}

print(type(a))  #<class 'str'>
print(type(b))  #<class 'int'>
print(type(c))  #<class 'bool'>
print(type(d))  #<class 'list'>
print(type(e))  #<class 'dict'>

<class 'str'>
<class 'int'>
<class 'bool'>
<class 'list'>
<class 'dict'>

3.标识符和关键字

  • 标识符:变量,模块名,函数名,类名;
  • 命名规则:
    • 由数字、字母和 _组成,不能以数字开头;
    • 严格区分大小写 (计算机编程里,一共有 52 个英语字母 a~z A~Z);
    • 不能使用 Python 关键字 (特殊含义的单词);
  • 规范:
    • 顾名思义;
    • 遵守命名规范;
      • 小驼峰命名法:第一个单词的首字母小写,往后每个单词的首字母都大写; userNameAndPasswd
      • 大驼峰命名法:每个单词首字母都大写;UserName
      • 使用下划线连接;user_name_and_passwd
      • Python 里的变量、函数和模块名使用下划线连接;类名使用大驼峰命名法;(Python 之父的命名规则)

4、输出语句,内置函数 print

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  • value 值,可以输出多个值。使用 , 隔开
>>> print('test', 'Name', 'hello')
test Name hello
  • sep 定义分隔符,默认使用 空格 分割
>>> print('test', 'Name', 'hello', sep='+')
test+Name+hello
  • end 默认执行完一个 print 输出语句后,再要输出的字符,默认为 \n 换行再执行。
>>> print('test', 'Name', 'hello', sep='+')
>>> print('test', 'Name', 'hello')
test+Name+hello
test Name hello

>>> print('test', 'Name', 'hello', sep='+', end='=')
>>> print('test', 'Name', 'hello')
test+Name+hello=test Name hello
  • file 要写入的文件对象,默认输出至 控制台
  • flush 输出是否被缓存通常决定于 file,但如果 flush 关键字参数为 True,流会被强制刷新。

5、输入语句,内置函数 input

>>> name = input("Please input your Name:")
>>> print(name)
Please input your Name:>? Jarbo
Jarbo
#不管输入内容是什么,变量保存的结果都是字符串
>>> age = input('Please Input Your Age:')
>>> print(age)
>>> print(type(age))
Please Input Your Age:18
18
<class 'str'>

未完~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

712 0 2021-01-17


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-04-28

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 216621 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!