关于python:Python入门系列三一学就会基础数据类型

37次阅读

共计 3177 个字符,预计需要花费 8 分钟才能阅读完成。

数据类型

您能够应用 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 算术运算符

Operator Name Example
+ Addition x + y
Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y

赋值运算符

Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x – 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x = x & 3
\ = x \ = 3 x = x \ 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

比拟运算符

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

逻辑运算符

Operator Description Example
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

身份运算符

Operator Description Example
is Returns True if both variables are the same object x is y
is not Returns True if both variables are not the same object x is not y

成员运算符

Operator Description Example
in Returns True if a sequence with the specified value is present in the object x in y
not in Returns True if a sequence with the specified value is not present in the object x not in y

位运算符

Operator Name Description
& AND Sets each bit to 1 if both bits are 1
\ OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

正文完
 0