当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件。要运行代码,就需要Python解释器去执行.py文件
在写代码之前,请千万不要用“复制”-“粘贴”把代码从页面粘贴到你自己的电脑上。写程序也讲究一个感觉,你需要一个字母一个字母地把代码自己敲进去,在敲代码的过程中,初学者经常会敲错代码,所以,你需要仔细地检查、对照,才能以最快的速度掌握如何写程序。
输出
字符串:
print(“hello, world”) 则输出hello, world
等价于print(‘hello, world’)。
双引号和单引号在这里是一样的。
多个字符串:
print(“hello word”, “i love you”, ‘or you’)
遇到逗号的时候会自动输出一个空格
name = input(‘please enter your name: ‘)
print(‘hello,’, name)
先输出please enter your name
然后等待从键盘上输入name,最后再输出hello, name的值
#开头的是注释
以冒号结尾的表明这是个代码块
对于浮点数用e代表10
and是与运算 or是或运算 not是非运算
空值是None 不是0 0有意义,None是一个特殊的值
//python中称为地板除,省略小数,向下取整
print(10 // 3)
> 3
s3 = 'i can "love" you'
print(s3)
> i can "love" you
外面用双引号里边的单引号不用转义字符,同理外面用单引号,里面的双引号也不用转义字符。
s1 = 'hello\nworld'
print(s1)
> hello
world
\n相当于回车
s1 = r'hello\nworld'
print(s1)
hello\nworld
>
前面有r将引号中的内容输出来,内部字符串默认不转义。
s2 = “hello,\
world”等价于s2 = “hello, world” 当要写成两行的时候则要用一个\来连接上下行
s3 = ”’hello,
world,
hahaha”’
等价于
s3 = ‘hello,\nworld,\nhahaha’
外面用双引号里边的单引号不用转义字符
s3 = ‘i can “love” you’
在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。
words = 'for you ' * 3
print(words)
> for you for you for you
name = ‘My Name is Mike’
print(name[0]) R:M
print(name[-4])从后面数第四个 R:M
print(name[11:14]) R:Mik
print(name[11:15]) R: Mike
print(name[5:]) R:me is Mike
print(name[: 5]) R:My Na
[x , y]从x起到y,不包括y
注意的一点是从0开始数的,不是从1
phone_number = '13866660006'
hiding_number = phone_number.replace(phone_number[3:7],'*' * 4)
print(hiding_number)
R:138****0006
replace进行遮挡第一个表示要被遮挡的内容,后面的表示用什么遮挡
search = '168'
num_a = '1386-168-0006'
num_b = '1681-789-0006'
print(search + ' is at ' + str(num_a.find(search)) + ' to ' + str(num_a.find(search) + len(search)) + ' of num_a ')
print(search + ' is at ' + str(num_b.find(search)) + ' to ' + str(num_b.find(search) + len(search)) + ' of num_b ')
R:
168 is at 5 to 8 of num_a
168 is at 0 to 3 of num_b
print('{} a word she can get what she {} for.'.format('With','came'))
print('{preposition} a word she can get what she {verb} for'.format(preposition = 'With', verb = 'came'))
print('{0} a word she can get what she {1} for.'.format('With','came'))
R:
With a word she can get what she came for.
With a word she can get what she came for
With a word she can get what she came for.
desktop_path = 'D:\Desktop'//桌面的路径
full_path = desktop_path + '/'+name + '.txt'
file = open(full_path,'w')//打开此路径,没有则创建一个
file.write(msg)//将msg的内容写到路径中
file.close()//关闭文件
text_create('hello','hello world i')
str = "this is string example....wow!!! this is really string"
print (str.replace("is", "was"))
print (str.replace("is", "was", 2))
R:
thwas was string example….wow!!! thwas was really string
thwas was string example….wow!!! this is really string
将str中的is全部用was代替
str中的is被was代替两次
def text_filter(word,censored_word = 'lame',changed_word = 'Awesome'):#替换过滤字
return word.replace(censored_word,changed_word)
def text_cencored_create(name,msg):
cleanmsg = text_filter(msg)
desktop_path = 'D:\Desktop'
full_path = desktop_path + '/' + name + '.txt'
file = open(full_path,'w')
file.write(cleanmsg)
file.close()
text_cencored_create('Try','lame!lame!lame!lame')