Python 基础

1、基础

1.1、注释

其作用是给程序读者观看的笔记

  1. 单行注释:#(#右侧的内容都将被注释)
  2. 多行注释:”’ “”“(闭合的三引号)

1.2、字面常量

用的都是其字面上的值或者内容,其值或者不会改变

如5、3.12等

  • 数字

    1. 整数(integers):2

    2. 浮点数(floats):3.23、53.e-4

  • 字符串(string)

一串字符串是字符(characters)的序列(sequence),基本上字符串就是一串词汇,也可以是数字

  • 单引号

可用来指定字符串,如‘我是一串字符串’

  • 双引号

与单引号作用差不多,也可用来指定字符串

  • 三引号

可以是“ “ “ 也可以是”’,来指定多行字符串,可以在三引号中使用单、双引号

'''这是一段多行字符串,这是它的第一行
this is the second line
“what's your name?”, i asked.
he said "bond, james bond."

'''
  • 字符串不可变
  • 格式化方法(format()方法)

可从其他信息中构建字符串

age = 20
name = 'Bonds'

print('{0} was {1} years old when he wrote this book'.format(name, age))
print('why is {0} playing with that python?'.format(name))

------
输出:
Bonds was 20 years old when he wrote this book
why is Bonds playing with that python?

工作原理:

一个字符串可以使用某些特定的格式(specification),随后format方法被调用,使用这一方法中与之对应的参数替换这些格式。

在这里,{0}对应变量name,是该格式化方法中第一个参数,{1}对应变量age,是第二个参数。

python中format方法所做的事便是将每个参数替换至格式所在的位置,更详细的格式如下:

# 对于浮点数'0.333'保留小数点后三位,.3f指定了浮点数的精度为3
print('{0:.3f}'.format(1.0/3))
# 使用下划线填充文本,并保持文字处于在中间位置
# 使用^定义'__hello__'字符串长度为11
print('{0:_^11}'.format('hello'))
# 基于关键词输出'Swaroop wrote A Byte of Python'
print('{name} wrote {book}'.format(name='Swaroop', book='A Byte of Python') )

----
输出:
0.333
__hello__
Swaroop worte A Byte of Python

PS:print()方法总是以一个不可见的“新一行”字符(\n)结尾,每次打印都重新打印一行,为避免这种情况,可以通过end=指定其应以空白结尾:

print('a', end='')
print('b', end='')
print('c', end='')

----
输出:
abc

1.3、转义序列

当要输出一串带有单引号的字符串的时候,假如字符串本身也是使用的单引号指定,那么python则会报语法错误

如’what’s your name?’ ,因为python不知道何处是字符串的开始,哪里是结尾,有两种解决办法:

  1. 对字符串中的单引号,使用转义符号(\)进行转义
# 在单引号前面加以\转义
print('what\'s your name?')
输出:
what's your name
  1. 利用python既可以用双引号(”“)也可以用单引号(”)指定字符串的特征
print("what's your name?")

如何指定一串双行字符串?

  1. 使用三引号字符串
  2. 使用\n表示新的一行
print('this is the first line\nthis is second line')

在一个字符串末尾放置反斜杠\ ,表示字符串将在下一行继续,但不会添加新一行

str = 'this is the first sentence.  \
       this is second sentence.'
或者:
str1 = 'this is the first sentence.' \
        'this is the sentence.'
print(str)
----
输出:
this is the first sentence.         this is second sentence.
this is the first sentence.this is second sentence.

1.4、原始字符串(r)

在字符串本身带有反斜杠(\)的时候,有时打印出来的结果并不是我们所预期的

# 并不是我们想要的结果
str = 'c:\now'
print(str)
c:
ow
# 使用原始字符串(r)进行转义
str = r'c:\now\fish'
print(str)
c:\now\fish

在处理正则表达式时应全程使用原始字符串,否则,将会有大量backwhacking需要处理,举例说明的话,反向引用可以通过’\1’或r’1’来实现。

1.5、变量(variables)

变量的值是可以变换的,可以存储任何东西,需要通过一些方式访问,因此需要为它命名。

1.6、标识符命名(identifiers)

变量是标识符的一个例子,是为某些东西提供给定名称,在命名时需要遵循以下规则:

  1. 第一个字符(即头字符),只能是字母或者下划线,不能是数字
  2. 标识符其它部分,可以是字母、数字、下划线
  3. 标识符区分大小写
# 有效的标识符
i、name_2_3
# 无效的标识符
2things、this is spaced out、my-name和>a1b2_c3

1.7、数据类型(data type)

变量可以将各种形式的值保存为不同的数据类型,基本的数据类型有:

  1. 整型
  2. 浮点型
  3. 布尔类型

变量可以将各种形式的值保存为不同的数据类型

1.8、物理行(physical line)和逻辑行(logical line)

physical line就是你编写程序时看到的内容,logical line就是你所看到的单个语句,python假定没有一个logical line对应一个physical line

诸如print(‘hello world’)这样一句语句,如果本身是一行(正如编辑器里看到的一样),那么它也对应着一行physical line。

强烈建议每一个physical line只写一行logical line,使程序更加可读,对于长字符串可以使用反斜杠将其拆分为多个physical line,称之为显示行连接(explicit line joining)

s = 'this is first string. \
this is second string.'
print(s)
----输出
this is first string. this is second string.

1.9、缩进

python官方建议四个空格来缩进,也可以使用制表符(tab),放置在一起的语句必须有同样的缩进,这样的语句被成为block(块)。

i = 5
    print('value is:', i)
File "C:/Users/hj/PycharmProjects/untitled1/test3.py", line 27
print('value is:', i)
    ^
IndentationError: unexpected indent  # 缩进错误:意外的缩进
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风老魔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值