【Python学习】1-1安装与(基础知识-1)

发布时间:2024年01月14日

声明:

本人从菜鸟教程学习的Python,所以和那里的知识差不多。

Python初学者,请路过的大佬多多指教啦~

零、安装

????????参考了这位姐姐的视频,不拖沓、视频画面简洁美观、声音好听、声音好听、声音还好听。

你觉得自己这辈子都学不会编程?超超超基础Python课程,3小时快速入门 【自学Python教程合集】【3小时快速入门Python】

????????IDE使用了PyCharm,因为用过IDEA所以PyCharm用起来上手也很快。

①插件推荐

1.中文语言包

Chinese (Simplified) Language Pack / 中文语言包

2.Smart Input

智能切换输入法,我的IDEA也在用这个插件。

Smart Input

一、基础知识

①字符串

????????python没有字符类型,单个字符其实是长度为一的字符串。

str0 = '0123456789'

print("输出整个字符串:str0 = " + str0)
print("下标为0的字符:str0[0] = " + str0[0])
print("下标0到2(3之前)的字符串:str0[0:3] = " + str0[0:3])
print("下标0到-2(-1之前)的字符串:str0[0:-1] = " + str0[0:-1])
print("4及之后所有字符:str0[4:] = " + str0[4:])
print("字符串输出两遍:(str0 + '|')*2 = " + (str0 + '|') * 2)
print("从下标2输出到下标8,步长为3(即输出str0[2]、str0[2+3]、str0[2+3+3]):str0[2:9:3] = " + str0[2:9:3])

????????输出结果:

输出整个字符串:str0 = 0123456789
下标为0的字符:str0[0] = 0
下标0到2(3之前)的字符串:str0[0:3] = 012
下标0到-2(-1之前)的字符串:str0[0:-1] = 012345678
4及之后所有字符:str0[4:] = 456789
字符串输出两遍:(str0 + '|')*2 = 0123456789|0123456789|
从下标2输出到下标8,步长为3(即输出str0[2]、str0[2+3]、str0[2+3+3]):str0[2:9:3] = 258

②转义符‘\’

????????在字符串前面加r可以使该字符串不发生转义

print("hello\tjojo")
print(r"hello\tjojo")

????????输出结果:

hello	jojo
hello\tjojo

③一行多语句与一语句多行

# 在同一行写多条语句,使用分号分割
x = "hello"; print("x = " + x)
y = 123; print("y = " + str(y))

# 在多行写一个语句,使用反斜杠\连接
str1 = "Hello," + \
       "Python " + \
       "is " + \
       "concise!"

# 写在 [], {}, 或 () 中的多行语句,不需要使用反斜杠 \ 连接
str2 = ["Hello,",
        "Python ", "is ",
        "concise!"]
print(str1)

④数字类型?

????????python中有四种数字类型:整数、布尔型、浮点数、复数

a = 114514
b = True
c = 114.514
d = 1.3 + 3j # 复数有这两种写法
e = complex(2,-1) # 复数有这两种写法

print("a的类型是:", type(a))
print("b的类型是:", type(b))
print("c的类型是:", type(c))
print("d的类型是:", type(d))
print("e的类型是:", type(e))

????????输出结果:

a的类型是: <class 'int'>
b的类型是: <class 'bool'>
c的类型是: <class 'float'>
d的类型是: <class 'complex'>
e的类型是: <class 'complex'>

⑤数据类型转换

# 转字符串 str()
a = 114514; b = True
print("a = " + str(a) + "\nb = " + str(b))
print("str(a)的类型是:",type(str(a)))

# 转数字 int() float() ord() oct() hex()
c = 114.514; e = complex(2,-1)
print(float(str(c)))
print(int(str(a)))
print(ord('a')) # 将字符转为Unicode值
print(oct(a)) # 整数转八进制
print(hex(a)) # 整数转十六进制

?????????输出结果:

a = 114514
b = True
str(a)的类型是: <class 'str'>
114.514
114514
97
0o337522
0x1bf52

⑥print不换行?

????????Python中的print默认是换行的,想不换行(不以'\n'结尾)或者以其它什么结尾可以这样写:

print("test", end="")
print("Hello!", end=" 喵~\n")
print("Python!", end=" 喵~")

????????输出结果:

testHello! 喵~
Python! 喵~

文章来源:https://blog.csdn.net/qq_73357884/article/details/135575707
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。