第二章 变量和简单数据类型
2.1 输出一个hello world
print("hello world")
#用#进行注释
2.2 变量的定义
num1 = 1 #整型
num2 = 1.0 #浮点型
str1 = 'abcd' #字符串型
str2 = "abcd" #字符串型
bool1 = True #布尔型
直接用 变量名=变量值 来定义,系统自动检测变量的类型
2.3.1 使用方法修改字符串的大小写(title,upper,lower)
name = "i loVe pYthon"
print(name.upper()) #临时转为大写
print(name.lower()) #临时转为小写
print(name.title()) #临时首字母大写
2.3.2 合并字符串(str + str)
first_name = "abc"
last_name = "def"
name = first_name + last_name
print(name)
================ 运行结果 ================
abcdef
>>>
2.3.4 删除空白(str.strip() str.lstrip() str.rstrip() )
c = " python "
print("."+c.rstrip()+".") #删除右边的空白
print("."+c.lstrip()+".") #删除左边的空白
print("."+c.strip()+".") #删除两边的空格
================ 运行结果 ================
. python.
.python .
.python.
>>>
第三章 列表简介
3.1 建立并访问列表
arr = ['a','b','c']
print(arr[0]) #类似于C,用下标的方式寻找
print(arr[-2]) #访问倒数第二个元素
3.2 修改添加和删除元素
1.修改元素
s = ["abc","def"]
s[0] = "ghi" # 修改
>>>
2.添加元素(添加append、插入insert)
s = ["a","b","c","d"]
s.append("添加了e")
s.insert(1,"插入了python")
print(s)
============= RESTART: C:\Users\Administrator\Desktop\python.py =============
['a', '插入了python', 'b', 'c',