Ubuntu16.04 Python2.7起步(一)

Ubuntu16.04 Python2.7 起步

前序

当自己迷惘或者困惑的时候,就去读书和运动;改变自己内在精神和外在的肉体,放弃自己关注的东西,不在关注乱七八糟的新闻,放下下手机,抖音,快手,购物;让自己从内到外的放松下来,专注于当下,专注于自身;

从今天起,定一个小目标,和大家一同从python小白开始,学习python,解决日常杂事,让自己的人生开始一种脚本化运行;

安装python

找到python网站,下载安装python环境,然后畅游python学习,解决日常杂事;Python网站

安装python2.7

下载地方,可以选择其他版本 https://www.python.org/downloads/source/ ,我采用ubuntu自带的python版本

sudo apt-get install python2.7
cooper@cooper:~$ python
Python 2.7.12 (default, Mar  1 2021, 11:38:31) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

安装python pip工具

sudo apt-get install python-pip

这样我们完成了python的安装,可以开始python之旅了;

Helloworld

第一步我们开始从helloworld开始;每一种语言都是从最简单的开始,尝试使用print看看会有什么结果;

>>> print "Hello world!"
Hello world!
>>> 

简单加减乘除运算

cooper@cooper:~$ python
Python 2.7.12 (default, Mar  1 2021, 11:38:31) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 2+2
4
>>> 3*4
12
>>> 4/3
1
>>> 4%3
1
>>> 4-2
2
>>> 2-4
-2
>>> 

python2.7 +, -, * 和 / () 之类的应用;可以将python当做一个计算器使用

>>> 17/3        # 取整
5
>>> 17/3.0      # 算出真实浮点值
5.666666666666667
>>> 17//3       # // 和 "/" 没有区别
5
>>> 17%3        # 取余
2
>>> 5*3+2 
17
>>> 8**3        # 8的三次方
512
>>> 5**2        # 5的平方
25
>>> 2**7        # 2的平方
128
>>> 
>>> w=20        # 用 “=”号赋值
>>> h=5*9
>>> w*h
900
>>> 
>>> n            # 没有定义将产生一个error
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
>>> n=5         # 定义后,将没有问题;
>>> n
5
>>> 

在交互式模式下,最后一个打印的表达式被分配到可变 _。这意味着,当您使用 Python 作为台式计算器时,继续计算会稍微容易一些,例如:

>>> tax =12.5/100
>>> price = 100.50
>>> price +_
105.5
>>> price +_
206.0
>>> round(_,2)
206.0

除了 int 和 float,Python 还支持其他类型的数字,如十进制和分数。

>>> 0xfa
250
>>> 010
8
>>> 

字符串

(’…’) 和 ("…")有着不太一样的含义,请看下面的例子

>>> 'can't'         # 语法错误,未有转义字符
  File "<stdin>", line 1
    'can't'
         ^
SyntaxError: invalid syntax
>>> "can't"         # 解决这种错误也可以用“”号;
"can't"
>>> 'can\'t'
"can't"             # 增加转义字符
>>> 

\n 为转义字符,具备特殊含义,看一下接下来的案例

>>> print('C:\some\name')  # 这个 \n 意思是换行!
C:\some
ame
>>> print(r'C:\some\name')  # 解决这个问题可以加一个r
C:\some\name

在看一下接下里的例子

>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.'  # \n means newline
>>> s  # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s)  # with print(), \n produces a new line
First line.
Second line.

字符串 可以结合 用 + 操作符, 也可以用 * 重复字符:

>>> 3*"op"+"q"
'opopopq'
>>> 

结束

今天暂时学习到这里,从这简单的操作中,我们能够基本认识Python的基本语法,从数字运算到字符串操作;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值