笨方法学习Python(1-10)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了笨方法学习Python(1-10)相关的知识,希望对你有一定的参考价值。
以下学习内容以python2为基准
UTF-8
#conding:utf-8 or #__coding:utf-8__
此句要置顶,表示代码支持UTF8的格式,最好每个代码文件都加上
注释
# A comment, this is so you can read your program later.
代码前加“#”表示的是注释,以后写每行代码的上一行记得都加上解释信息
python2与python3
print“abc” #python2的写法 print ("abc") #python3的写法
数字和数学计算
+ plus 加号
- minus 减号
/ slash 斜杠
* asterisk 星号
% percent 百分号
< less-than 小于号
> greater-than 大于号
<= less-than-equal 小于等于号
>= greater-than-equal 大于等于号
变量和命名
>>> cars = 100 >>> print "There are", cars, "cars available."
结果: There are 100 cars available.
更多的变量和打印
>>> my_name = 'Kavin' >>> print "Let's talk about %s." % my_name
结果
>>> Let's talk about Kavin.
python中的%r和%s
%r用rper()方法处理对象
%s用str()方法处理对象
有些情况下,两者处理的结果是一样的,比如说处理int型对象
%r会在字符串两侧多出‘ ’
字符串(srring)和文本
w = "This is the left side of..." e = "a string with a right side." print w + e This is the left side of...a string with a right side. print "." * 10 ..........
打印
days = "Mon Tue Wed Thu Fri Sat Sun" months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" print "Here are the days: ", days print "Here are the months: ", months print """ There's something going on here. With the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. """ $ python ex9.py Here are the days: Mon Tue Wed Thu Fri Sat Sun Here are the months: Jan Feb Mar Apr May Jun Jul Aug There's something going on here. With the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6.
\n 强制换行
print """ """ 按照既定的格式显示
''' ''' 也可使用单引号
print "",abc 加逗号,逗号后面的不换行
那是什么
\ 转义符,可将难打印出来的字符放到字符串
\t \r \n都是转义字符,空格就是单纯的空格,输入时可以输入空格
\t 的意思是 横向跳到下一制表符位置
\r 的意思是 回车
\n 的意思是回车换行
以上是关于笨方法学习Python(1-10)的主要内容,如果未能解决你的问题,请参考以下文章