Python入门系列(三)一学就会-基础数据类型

数据类型

您可以使用type()函数获取任何对象的数据类型。

x = 5
print(type(x))

数字类型

x = 1    # int
y = 2.8  # float
z = 1j   # complex

Int,或integer,是一个长度不限的整数,正数或负数,不带小数。

x = 1
y = 35656222554887711
z = -3255522

浮点数,或“浮点数”是一个包含一个或多个小数的正数或负数。

x = 1.10
y = 1.0
z = -35.59

浮点数也可以是科学数字,用“e”表示10的幂。

x = 35e3
y = 12E4
z = -87.7e100

复数是用“j”作为虚部写成的

x = 3+5j
y = 5j
z = -5j

您可以使用int()、float()和complex()方法从一种类型转换为另一种类型

x = 1    # int
y = 2.8  # float
z = 1j   # complex

#convert from int to float:
a = float(x)

#convert from float to int:
b = int(y)

#convert from int to complex:
c = complex(x)

Python没有生成随机数的random()函数,但Python有一个内置的模块,名为random,可用于生成随机数

import random

print(random.randrange(1, 10))

字符串

可以使用三个引号将多行字符串指定变量

a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)

要获得字符串的长度,请使用len()函数。

a = "Hello, World!"
print(len(a))

要检查字符串中是否存在某个短语或字符,可以在中使用关键字in或者not in

txt = "The best things in life are free!"
print("free" in txt)
txt = "The best things in life are free!"
print("expensive" not in txt)

upper()方法以大写形式返回字符串

a = "Hello, World!"
print(a.upper())

lower()方法以小写形式返回字符串

a = "Hello, World!"
print(a.lower())

strip()方法从开头或结尾删除任何空格

a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"

replace()方法将一个字符串替换为另一个字符串

a = "Hello, World!"
print(a.replace("H", "J"))

split()方法返回一个列表,其中指定分隔符之间的文本成为列表项。

a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']

要连接或组合两个字符串,可以使用+运算符。

a = "Hello"
b = "World"
c = a + b
print(c)

format()方法接受传递的参数,对其进行格式化,并将其放置在占位符{}所在的字符串中

age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))

format()方法接受无限数量的参数,并放置在相应的占位符中:

quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))

您可以使用索引号{0},以确保参数放置在正确的占位符中

quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))

要在字符串中插入非法字符,请使用转义字符。

txt = "We are the so-called \"Vikings\" from the north."

布尔值

除了空值(如()、[]、{}、“、数字0和值None)之外,没有多少值的计算结果为False。当然,值False的计算结果为False。

# 下面将返回False
bool(False)
bool(None)
bool(0)
bool("")
bool(())
bool([])
bool({})

Python运算符

Python算术运算符

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor divisionx // y

赋值运算符

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

比较运算符

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

逻辑运算符

OperatorDescriptionExample
andReturns True if both statements are truex < 5 and x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)

身份运算符

OperatorDescriptionExample
isReturns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectx is not y

成员运算符

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objectx in y
not inReturns True if a sequence with the specified value is not present in the objectx not in y

位运算符

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值