学习python第三天

一.数据类型

1.获取数据类型
x = 10
print(type(x))


"""
输出
<class 'int'>
"""

 

2.复数类型(complex)详解

复数(Complex)是 Python 的内置类型,直接书写即可。换句话说,Python 语言本身就支持复数,而不依赖于标准库或者第三方库。

复数由实部(real)和虚部(imag)构成,在 Python 中,复数的虚部以j或者J作为后缀,具体格式为:a + bj

a 表示实部,b 表示虚部。

二.类型转换

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

# 把整数转换为浮点数
a = float(x)
# 把浮点数转换为整数 不会四舍五入,取整
b = int(y)
# 把整数转换为复数:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
"""
输出
10.0
6
(10+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
"""

注释:您无法将复数转换为其他数字类型。

三.随机数

import random
print(random.randrange(1,10))
"""
输出
1-10之间的一个数
"""

四.字符串

1.字符串字面量

python 中的字符串字面量由单引号或双引号括起

'hello' 等同于 "hello"。

您可以使用 print() 函数显示字符串字面量:

print("Hello")
print('Hello')
2.用字符串向变量赋值
a = "Hello"
print(a)
3.多行字符串

实例

您可以使用三个双引号:

a = """Python is a widely used general-purpose, high level programming language. 
It was initially designed by Guido van Rossum in 1991 
and developed by Python Software Foundation. 
It was mainly developed for emphasis on code readability, 
and its syntax allows programmers to express concepts in fewer lines of code."""
print(a)


"""
输出
Python is a widely used general-purpose, high level programming language. 
It was initially designed by Guido van Rossum in 1991 
and developed by Python Software Foundation. 
It was mainly developed for emphasis on code readability, 
and its syntax allows programmers to express concepts in fewer lines of code.
"""
4.字符串是数组

Python 没有字符数据类型,单个字符就是长度为 1 的字符串。

获取位置 1 处的字符(请记住第一个字符的位置为 0):

a = "Hello, World!"
print(a[1])
"""
输出
e
"""

五.裁切

您可以使用裁切语法返回一定范围的字符。

指定开始索引和结束索引,以冒号分隔,以返回字符串的一部分。

实例
获取从位置 2 到位置 5(不包括)的字符:

b = "Hello, World!"
print(b[2:5])
"""
输出
llo
"""

负的索引

获取从位置 5 到位置 1 的字符,从字符串末尾开始计数:

实例 
b = "Hello, World!"
print(b[-5:-2])
"""
输出
orl
"""

六.函数

1.strip()

方法删除开头和结尾的空白字符:

a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"
"""
输出
Hello, World!
"""
2.字符串长度

如需获取字符串的长度,请使用 len() 函数

a = "Hello, World!"
print(len(a))
"""
输出
13
"""
3.字符串小写

lower() 返回小写的字符串

a = "Hello, World!"
print(a.lower())
"""
输出
13
"""
4.字符串大写

upper() 方法返回大写的字符串:

a = "Hello, World!"
print(a.upper())
"""
输出
HELLO, WORLD!
"""
5.字符串替换

replace() 用另一段字符串来替换字符串:

a = "Hello, World!"
print(a.replace("World", "Kitty"))
"""
输出
Hello, Kitty!
"""

相关推荐

  1. 学习python

    2024-02-01 22:12:03       27 阅读
  2. 学习Python

    2024-02-01 22:12:03       10 阅读
  3. Python学习笔记七十(OpenCV简介)

    2024-02-01 22:12:03       41 阅读
  4. 每天学习python30分钟(

    2024-02-01 22:12:03       17 阅读
  5. 学习记录

    2024-02-01 22:12:03       40 阅读
  6. 前端学习

    2024-02-01 22:12:03       49 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 22:12:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 22:12:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 22:12:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 22:12:03       18 阅读

热门阅读

  1. k8s学习-Health Check

    2024-02-01 22:12:03       30 阅读
  2. 网课:校门外的树——牛客(题解)

    2024-02-01 22:12:03       40 阅读
  3. Spring相关框架中的bean及其实例化

    2024-02-01 22:12:03       29 阅读
  4. Ubuntu 22.04开发板更新源报错404 Not Found

    2024-02-01 22:12:03       25 阅读
  5. XML详解

    XML详解

    2024-02-01 22:12:03      38 阅读
  6. 2024-01-31-好的技术文章汇总

    2024-02-01 22:12:03       33 阅读
  7. 【无标题】

    2024-02-01 22:12:03       32 阅读
  8. Unity_Visual Effect Graph

    2024-02-01 22:12:03       28 阅读
  9. web应用课——(第五讲:React)

    2024-02-01 22:12:03       30 阅读