Python 变量和简单数据类型 2

1. print 打印

print("Hello Python world!")

2.变量

message = "Hello Python world!"
print(message)

3.字符串 可以是单引号也可以是双引号

"This is a string."
'This is also a string.'

 4.字符串大小写转换

name = "Ada Lovelace"
print(name.upper())
print(name.lower())

5.字符串中使用变量  f代表format

first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)

6.字符串中添加制表符,可使用字符串组合\t

c="\tabc"
print (c)

 

7.字符串  换行使用\t

print("Languages:\n\tPython\n\tC\n\tJavaScript")

 

 8.字符串去空白(依次是去右边,去左边,去两边)

favorite_language=' pyhton '
str1=favorite_language.rstrip()
print(str1)
str2=favorite_language.lstrip()
print(str2)
str3=favorite_language.strip()
print(str3)

9. 整数

# 整数
a=2; b=3
print(a+b)

#整数没有限制大小
a=11121212132123123123223423423423423432423
b=23423423423423423423423423423423423423423
print(a+b)

#转整数
c="23"
d="23"
print(int(c)+int(d))

10.浮点数

#浮点数
a=0.2; b=0.1
print(a+b)

#转浮点数
c="1212.12"
d="1212.12"
e=float(c)+float(d)
print(e)

11.常量

  在高级语言中,常量类似于变量,但值在程序整个生命周期内保持不变, python是没有内置的常量类型,但开发时可能需要, 定义常量时,约定变量名用大写视为常量,当开发人员看到大写的变量时,就不去重新赋值,这是一种约定。

MAX_CONNECTIONS=5000
print(MAX_CONNECTIONS)

 

12.数字下划线

#数字下划线,更好的可读性,python3.6+支持
universe_age=14_000_000_000
print(universe_age)

输出
14000000000

 

13.除法运算符 /  和 // 介绍

#除法/总是返回一个浮点数,如果只想要整数,使用运算符//
a=10; b=2
print(a/b)
print(a//b)

#整数除法返回浮点型
print(17/3)
#返回整数
print(17//3)

#注意使用 //运算符时, 只有整数与整数运算才能返回整数,否则返回浮点数
print(7//2)
print(7.0//2)
print(7//2.0)

 

14.bool 布尔类型

a=True
b=False
if a!=b:
    print('不相等')
else:
    print('相等')

15.字符串截取

  下面一段代码是截取token值

body='"hello": getSearchToken("1","6jQFmiGx9FOZ2iubiELVukGjhtiqN5aPA468wlEcaVrD9IUdZ4"),'
start=body.find('getSearchToken')
if start==-1:
    print('未找到getSearchToken!')
start+=20
end=start+53
newbody=body[start:end].replace('"),','')
print(newbody)

16 查看字符串编码

    下面自动化转成16进制,再转成10进制,最后再转成2进制

     可以发现 “自”的二进制为: 11101000 、10000111、11100101

Python 3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> name="自动化"
>>> v1=name.encode("utf-8")
>>> v1
b'\xe8\x87\xaa\xe5\x8a\xa8\xe5\x8c\x96'
>>>
>>> v1[0]
232
>>> v1[1]
135
>>> v1[2]
229                      
>>> bin(232)
'0b11101000'
>>> bin(135)
'0b10000111'
>>> bin(229)
'0b11100101'
>>>

 

posted on 2022-08-04 11:23  花阴偷移  阅读(33)  评论(0)    收藏  举报

导航