9 月 152020
 
#字符串赋值
word = 'a loooooong word'
#整数赋值
num = 12
#字符串赋值
string = 'bang!'
#字符串运算
total = string * (len(word) - num)
#total = 'bang!' * (len(word) - 12)
#total = 'bang!' * (16 - 12)
#len函数,计算字符串长度
print(total)
#连续四次
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
bang!bang!bang!bang!
PS C:\Users\harveymei> 
9 月 152020
 
#变量数据类型,整数integer
num = 1
#变量数据类型,字符串string
string = '1'
#不同数据类型无法合并
print(num + string)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Traceback (most recent call last):
  File "c:/Users/harveymei/hello.py", line 6, in <module>
    print(num + string)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
PS C:\Users\harveymei>

 

#变量数据类型,整数integer
num = 1
#变量数据类型,字符串string
string = '1'
#转换数据类型
num2 = int(string)

print(num + num2)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
2
PS C:\Users\harveymei> 
9 月 152020
 
first = "任何在双引号之间的字符,"
second = '或者任何在单引号之间的字符,都是字符串。'
third = '''三引号之间的内容,可以在
完成所有内容之前换行。'''
#字符串基本用法
#字符串作为值赋予变量
what_he_does = ' plays'
his_instrument = ' guitar'
his_name = 'Robert Johnson'
artist_intro = his_name + what_he_does + his_instrument
#打印变量
print(artist_intro)
print(first + second + third)
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Robert Johnson plays guitar
任何在双引号之间的字符,或者任何在单引号之间的字符,都是字符串。三引号之间的内容,可以在
完成所有内容之前换行。
PS C:\Users\harveymei>
9 月 152020
 
# -*- coding:utf-8 -*-
#指定字符集
file = open('C:/Users/harveymei/Desktop/newfile.txt','w')
file.write('将内容写入文件')
#在Windows的文件系统路径中,应当使用/斜线而不是\斜线
#打开一个文件,如果不存在就创建,对文件执行写入
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
PS C:\Users\harveymei> cat C:/Users/harveymei/Desktop/newfile.txt
将内容写入文件
PS C:\Users\harveymei>
9 月 152020
 
a = 100
A = 200
print(a)
print(A)
#对变量赋值同时打印变量值
#python语言对大小写敏感,因此变量a和A时两个不同的变量
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
100
200
PS C:\Users\harveymei>
9 月 152020
 
# -*- coding: utf-8 -*-
print ("Hello World!")
print ("hello Again!")
print ("这是python3的print函数")
print ("与python2的不同之处在于需要在字符串之外增加括号")
# 使用#号来注释
# 使用UTF-8编码避免乱码
PS C:\Users\harveymei> & C:/Users/harveymei/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harveymei/hello.py
Hello World!
hello Again!
这是python3的print函数
与python2的不同之处在于需要在字符串之外增加括号
PS C:\Users\harveymei>