首页 > 编程语言 >Python入门系列(三)一学就会-基础数据类型

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

时间:2022-08-28 18:12:13浏览次数:52  
标签:一学 Python Operator 数据类型 运算符 字符串 print World Hello

数据类型

您可以使用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,Operator,数据类型,运算符,字符串,print,World,Hello
From: https://www.cnblogs.com/bugs-in-life/p/16633285.html

相关文章

  • python版本
    一个系统上可以同时安装几个python版本,不必去卸载旧的python版本;ubuntu上安装新的python版本:https://blog.csdn.net/weixin_42256557/article/details/122342614安装完成......
  • python中的多线程与多进程
    线程概念:线程也叫轻量级进程,是操作系统能够进行运算调度的最小单位,它被包涵在进程之中,是进程中的实际运作单位。线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的......
  • python中的浅拷贝与深拷贝
    1.python中的数据类型分为两种:不可变数据类型:数值number,字符串String,元组tuple可变数据类型:字典dic,列表list,集合set2.定义深拷贝:拷贝的程度深,开辟了一块新......
  • 3,python3 windows 安装,及 windows python 环境 requests模块安装
    1,安装python环境1,执行安装包,双击->python-3.10.4-amd64.exe->勾选选自定义安装和勾选添加环境变量  2,勾选安装所有用户和设置安装路径  3,cmd->python,验证Py......
  • Python Selenium使用cookie实现自动登录微博
    @目录前言一、预登陆获取cookie1)cookie处理2)预登陆二、登录测试前言模拟登录微博是实现微博网页爬虫的第一步,现在的微博网页版有个sinavisitsystem,只有登录过后才......
  • [转]Python PEP8 代码规范常见问题及解决方法
    转自:https://blog.csdn.net/qq_36759224/article/details/89304878 Python的PEP8代码规范,将常见的PEP8代码规范问题和解决方法记录一下,学习一下,养成良好......
  • 记录一次网站上传的python代替方法
    网址:https://tool2-mml.sjtu.edu.cn/VRprofile/VRprofile.php这个网站需要上传文件 思路抓包看下网络请求开始看的时候发现没啥然后用wireshark看了下发现文件上传......
  • 6.8 python基础列表元素的添加操作
     #append()list=[10,20,30,40,50,60,70,80]#列表后面添加元素,可以施单个元素或列表整体list.append(20)#后面添加单个元素print(list)list.append([30,90])#后面......
  • UE4 C++学习 浅析基本数据类型
    本文只解析一些UE4特有的一些数据类型,一些常用的类型如布尔(Bool),整型(Int)等不再赘述。 UE4的基本数据类型有以下几种:   命名(FName):在C++中,命名被写成FName使用......
  • python读取csv文件
    参考此贴:csv格式文件之csv.DictReader()方法_booze-J的博客-CSDN博客_csv.dictreader官方帮助:csv—CSVFileReadingandWriting—Python3.10.6documentationcsv......