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算术运算符
| 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 |
Python入门系列(三)一学就会-基础数据类型的更多相关文章
- mybatis入门系列三之类型转换器
mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...
- C# 互操作性入门系列(三):平台调用中的数据封送处理
好文章搬用工模式启动ing ..... { 文章中已经包含了原文链接 就不再次粘贴了 言明 改文章是一个系列,但只收录了2篇,原因是 够用了 } --------------------------- ...
- [转]C# 互操作性入门系列(三):平台调用中的数据封送处理
参考网址:https://www.cnblogs.com/FongLuo/p/4512738.html C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列( ...
- Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict)
Python入门篇-封装与解构和高级数据类型集合(set)和字典(dict) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.封装和结构 #!/usr/bin/env pytho ...
- SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型
原文:SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server ...
- Python入门学习:1.变量和简单的数据类型
python入门学习:1.变量和简单的数据类型 关键点:变量.字符串.数字 1.1 变量的命名和使用1.2 字符串1.3 数字1.4 注释 1.1 变量的命名和使用 变量,顾名思义是一个可变的量, ...
- Python入门系列【附】进阶教程
如题,本篇将讲解Python提升之路:Python作为语法简单易学的语言,入门容易精通却很难,这是共识,那么为什么会有这样的共识?精通Python的难度在哪里? Python拥有简单.形象.直观的语法 ...
- Python基础教程系列目录,最全的Python入门系列教程!
Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. 在现在的工作及开发当中,Python的使用越来越广泛,为了方便大家的学习,Linux大学 特推出了 <Python基 ...
- ActiveMQ入门系列三:发布/订阅模式
在上一篇<ActiveMQ入门系列二:入门代码实例(点对点模式)>中提到了ActiveMQ中的两种模式:点对点模式(PTP)和发布/订阅模式(Pub & Sub),详细介绍了点对点 ...
- Python 入门(三)整数和浮点数+布尔类型
整数和浮点数 Python支持对整数和浮点数直接进行四则混合运算,运算规则和数学上的四则运算规则完全一致. 基本的运算: 1 + 2 + 3 # ==> 6 4 * 5 - 6 # ==> ...
随机推荐
- H2-Table CATALOGS not found
在使用 IntelliJ IDEA 2021.1.3 版本,使用默认配置连接 H2 数据库的时候,出现下面错误,项目里 H2 使用的版本为 2.0.202 . [42S02][42102] org.h ...
- React + Typescript领域初学者的常见问题和技巧
React + Typescript领域初学者的常见问题和技巧 创建一个联合类型的常量 Key const NAME = { HOGE: "hoge", FUGA: "f ...
- JS:Boolean
Boolean数据类型: 有两个值:true false Boolean会把不是Boolean的值变为Boolean值 var a = 1; var b = true; var c = 0; var ...
- CLOSE_WAIT过多解决方法
背景:windows server 现象:CLOSE_WAIT过多(几百个),导致端口被占用光了,其他服务无法运行 原因:由于KeepLive在Windows操作系统下默认是7200秒,也就是2个小时 ...
- linux web漏洞扫描arachni
1. 下载arachni https://www.arachni-scanner.com/download/下载Linux x86 64bit 2. 上次解压直接使用 tar xzf arachni- ...
- Redis布隆过滤器和布谷鸟过滤器
一.过滤器使用场景:比如有如下几个需求:1.原本有10亿个号码,现在又来了10万个号码,要快速准确判断这10万个号码是否在10亿个号码库中? 解决办法一:将10亿个号码存入数据库中,进行数据库查询,准 ...
- NC13822 Keep In Line
NC13822 Keep In Line 题目 题目描述 又到饭点了,SK同学靠着惯性走到了食堂,但长长的队伍顿时让他失去了食欲.突然,他注意到某个窗口前的队伍里明显存在插队的现象,于是他默默记录下了 ...
- JDBC:Statement问题
1.Statement问题 2.解决办法:通过PreparedStatement代替 实践: package com.dgd.test; import java.io.FileInputStrea ...
- 从零开始制作【立体键盘】,画UI免写CSS,【盲打练习】的交互逻辑只用了10来行表达式!
手把手教你从空白页面开始通过拖拉拽可视化的方式制作[立体键盘]的静态页面,不用手写一行CSS代码,全程只用10来行表达式就完成了[盲打练习]的交互逻辑. 整个过程在众触应用平台进行,快速直观. 最终U ...
- 【一本通提高树链剖分】「ZJOI2008」树的统计
[ZJOI2008]树的统计 题目描述 一棵树上有 n n n 个节点,编号分别为 1 1 1 到 n n n,每个节点都有一个权值 w w w. 我们将以下面的形式来要求你对这棵树完成一些操作: I ...