苍天有泪 发表于 2018-8-11 07:33:44

笨方法学习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的写法
  3、数字和数学计算
  +    plus 加号
  -    minus 减号
  /    slash 斜杠
  *    asterisk 星号
  %    percent 百分号
  <    less-than 小于号
  >    greater-than 大于号
  <=    less-than-equal 小于等于号
  >=    greater-than-equal 大于等于号
  4、变量和命名
>>> cars = 100  
>>> print &quot;There are&quot;, cars, &quot;cars available.&quot;
  结果: There are 100 cars available.
  5、更多的变量和打印
>>> my_name = 'Kavin'  
>>> print &quot;Let's talk about %s.&quot; % my_name
  结果
>>>Let's talk about Kavin.  python中的%r和%s
  %r用rper()方法处理对象
  %s用str()方法处理对象
  有些情况下,两者处理的结果是一样的,比如说处理int型对象
  %r会在字符串两侧多出‘ ’
  6、字符串(srring)和文本
w = &quot;This is the left side of...&quot;  
e = &quot;a string with a right side.&quot;
  
print w + e
  
This is the left side of...a string with a right side.
  

  
print &quot;.&quot; * 10
  
..........
  7、更多打印
days = &quot;Mon Tue Wed Thu Fri Sat Sun&quot;  
months = &quot;Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug&quot;
  
print &quot;Here are the days: &quot;, days
  
print &quot;Here are the months: &quot;, months
  
print &quot;&quot;&quot;
  
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.
  
&quot;&quot;&quot;
  
$ 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&quot;&quot;&quot;      &quot;&quot;&quot;    按照既定的格式显示
  '''''' 也可使用单引号
  print &quot;&quot;,abc      加逗号,逗号后面的不换行
  10、那是什么
  \    转义符,可将难打印出来的字符放到字符串
  \t \r \n都是转义字符,空格就是单纯的空格,输入时可以输入空格
  \t 的意思是 横向跳到下一制表符位置
  \r 的意思是 回车
  \n 的意思是回车换行
页: [1]
查看完整版本: 笨方法学习Python(1-10)