前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从零开始学Python-day2

从零开始学Python-day2

作者头像
py3study
发布2020-01-09 19:40:39
7620
发布2020-01-09 19:40:39
举报
文章被收录于专栏:python3python3python3

Python--Day2

学习要有定位,明确目标地去学习。---leaves

python01---基础语法

运维开发:

    这个岗位最近已经越来越火,作为一个刚毕业没两年的小青年,职位规划与目标都是迷茫的。仅此记录一下日常点滴。

编程:这本身就很难 ===>需要用心学习的一个技能。

学习编程的最佳方法【最佳姿势】:

一、事前准备

    1.准备电脑

    2.找些简单的教程(如:如何用Flask开发网站,如何制作网站爬虫,如何打开文件)

        =====>开始敲代码

    3.学会Python后,才需要结合书(买书建议豆瓣8.0以上)

    4.多练习,熟能生巧。==>书读百遍其义自见。

二、工具准备

    python编程编辑器  vim    sublime    pycharm 

三、练习代码

    目标:把python当作工具应用到运维工作中去,成为运维界高手。

四、Python的优势

    python语言的优势:易学、脚本,功能强大。缺点就是运行速度比java慢

    Python语言:

    1.上手简单;

    2.功能健全;

    3.语言生态系统完善,第三方库多;

    4.有众多大公司成功的案例(豆瓣)。可以应用到脚本、游戏、图形化、Web爬虫等方方面面。

Python学习之路

一、(Linux系统下)输入python命令进入交互界面

[root@xiaowei ~]# python
Python 2.7.9 (default, Oct 19 2016, 13:38:05) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

练习1   输出hello world
>>>print "hello world"
hello world

练习2   四则运算
In [2]: 2 + 4
Out[2]: 6
In [3]: 4*2
Out[3]: 8
In [4]: print "hello" + "world"
helloworld

二、Python的文件执行方法

(vim 编辑后使用python +文件名运行,但是文件名要以.py后缀结尾)

[root@xiaowei 01]# vim 01.py

print "hello world"

[root@xiaowei 01]# python 01.py

hello world

[root@xiaowei 01]#

三、Python基础知识

3.1.变量  用来存放数据

    In [8]: x = "hello world "

    In [9]: print x

    hello world

3.2 语句    ===>一行代码

    在首行加_*_  coding:utf-8 _*_ 后在.py程序中可以输入保存中文,防止不识别中文报错。

    与用户交互的函数raw_input()和input()

raw_input()	==>获取用户输入(主要用来测试,实际生产中更多的是从数据库中查询数据。)默认是字符串

input()         ===>需要带原始的数据结构,即输入字符串要带引号。

eg:
In [2]: x = raw_input("a digit: ")
a digit: 2

In [3]: type(x)
Out[3]: str

In [4]: a = input("a digit: ")
a digit 2

In [5]: type(a)
Out[5]: int

3.3 python的数据类型

    数字、字符串、布尔、列表(list)、字典(dict)、元组(tuple)

    逻辑控制    +++>根据情况不同执行不同代码

     循环  ====>一直不停执行 while  if   until

3.4 四则运算

    /  ===>整除
    %  ===>取余
 eg:
     In [6]: 8/3
     Out[6]: 2

     In [7]: 7%3
     Out[7]: 1

字符串的运算:==>字符串可以相乘和相加

单双引号没有区别,注意"\"为转义符

    '''三重引号'''===>三重引号忽略所有格式,里边可以使用所有符号

eg:
###转义符的使用
In [10]: print 'I\'m Bob'
I'm Bob
###字符串相加
In [11]: 
In [8]: print "hello  " + "world"
hello  world
###字符串相乘
In [9]: print "hello " * 4
hello hello hello hello 
###三重引号用法
In [11]: print '''
    ...: HI  Bob!
    ...: I'm Yaso.
    ...: say"Bay0"
    ...: '''

HI  Bob!
I'm Yaso.
say"Bay0"

3.5 字符串的格式化

    print "hello " +x  + "I am " + str(y) + "years old"。字符串的格式化主要为了解决这种输出繁琐且丑陋的方式

    有两种方式格式化:    (%)和(format)

eg:
###不使用字符串格式化的丑陋拼接。
In [23]: x = "Leaves" 
In [24]: y = 3
In [26]: print "hello " +x  + "I am " + str(y) + "years old"
hello LeavesI am 3years old
###使用%来格式化
In [27]: print "hello %s ,I am %s years old " %(x,y)
hello Leaves ,I am 3 years old
###使用format来格式化 
In [29]: print "hello {} ,I am {} years old ".format(x,y)
hello Leaves ,I am 3 years old
##字符串转换为数字类型
In [31]: a = '13'
In [32]: print int(a)
13
In [34]: print int(a) + 4
17
wKiom1gpxcGzfooXAABeNmfOgL0182.png
wKiom1gpxcGzfooXAABeNmfOgL0182.png
小练习:让用户输入两个数求平均值
[root@xiaowei 01]# cat 02.py 
x = raw_input("first digist: ")
y = raw_input("second digist: "
print (int(x)+int(y))/2.0
[root@xiaowei 01]# 
[root@xiaowei 01]# python 02.py 
first digist: 4
second digist: 5
4.5

3.6 流程控制 : 布尔值 True False

    与  and 

    或  or

    非  not

  注意:if  else 可嵌套(嵌套不要超过三层,以保证代码可读性)

伪代码流程控制

if  true  还是False:

    如果是True(False)执行这段代码

else :

    执行False(True)这些行代码

eg:流程控制
In [36]: if 2 > 3 :
    ...:     print "condition is  True"
    ...: else :
    ...:     print "condition is false"
    ...:     
condition is false

###具体流程控制例子
In [37]: name = "wd"
In [38]: age = 22
In [39]: if name == "wd":
...:     if age > 14:
...:         print "you are %s years old" % age
...:     else :
...:         print "Too young"
...: else :
...:     print "You are {}".format(name)

3.7 循环  while   for

    while、for用来实现循环

    跳出循环  break && continue 。

    break : 中断终止循环

    continue :跳过当前循环,循环没有终止

###break举例(跳出循环,中断并终止循环)
In [57]: i = 0
In [58]: while True:
    ...:     if i > 5:
    ...:         break
    ...:     print i
    ...:     i += 1
    ...:     
0
1
2
3
4
5

###continue举例(跳过当前循环,循环没有终止)
In [59]: arr = ['C','js','python','js','css','js','html', 'node']
In [61]: for i in arr:
    ...:     if i == "js":
    ...:         continue
    ...:     print i
    ...:     
C
python
css
html
node

##小练习:用户输入数字,判断是不是闰年
·如果是100倍数,要被400整除
·被4整除
·比如1900年不是闰年,2000、2004是闰年
·如果不是闰年,提示信息,并继续输入
[root@xiaowei 01]# cat 07.py 
while True:
	if (int(year) % 400 == 0):
		print "run nian "
		break
	elif (int(year) % 100 != 0) and (int(year) % 4 == 0) :
		print "run nian "
		break
	else :
		year = raw_input("please  input year: ")
[root@xiaowei 01]#
优化后代码如下:
while True:
	if ((int(year) % 100 != 0) and (int(year) % 4 == 0) ) or (int(year) % 400 == 0):
		print "run nian "
		break
	year = raw_input("please  input year: ")
[root@xiaowei 01]#

while循环伪代码

while 情况1:

    代码会一直被执行,直到情况1为False

eg:
In [40]: i = 1
In [41]: while i <= 20 :
...:     print i
...:     i += 1
...:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

####让用户一直输入数字,如果输入的是0,终止程序,打印所有数字的和并求出平均值
[root@xiaowei 01]# cat 03.py 
x = raw_input("shu ru shu zi :  ")
sum = 0 
num = 0.0
while x :
	print "x :" + x
	sum += int(x)
	x = raw_input("shu ru shu zi :  ")
	num = num + 1.0
	#print num
print sum/num
print sum
[root@xiaowei 01]# 
[root@xiaowei 01]# python 03.py 
shu ru shu zi :  1
x :1
shu ru shu zi :  2
x :2
shu ru shu zi :  3
x :3
shu ru shu zi :  
2.0
6
###
小练习4:存10000块钱,年利率是3.25%。求多少年后存款可以翻番
[root@xiaowei 01]# cat 05.py
n = 10000
time = 1
while n < 20000:
	n = (1+ 0.0325) * n
	time += 1
	
print time ,n
[root@xiaowei 01]# python 05.py
23 20210.6986788

for循环  ==>专门针对list  列表  dict字典等复杂数据结构的

小练习:遍历列表
In [46]: arr = ['one',2,'333','four']
In [47]: for i in arr:
...:         print i
one
2
333
four

3.8 初识列表和字典

list   列表 [] ====>有顺序的

dict字典 {} ===> 没有顺序的 ,结构为key -value  形式(所有数据统计的思路都是这样)

d = {'name' :"black"}
n [1]: d = {'name':"black",'age': 12}
In [2]: print d['name']
black
In [3]: d['age'] = 50 		#修改值
In [4]: d
Out[4]: {'age': 50, 'name': 'black'}
In [5]: d['newKey'] = "newValue"		#新增值
In [6]: d
Out[6]: {'age': 50, 'name': 'black', 'newKey': 'newValue'}



###小练习:所有数据统计的思路都是这样
统计出列表中arr =['js','C','js','python','js','C','js','python','C','js','python','css',\
'js','html','node' ]各个字符出现的次数。


代码如下:
In [7]: arr = ['js','C','js','python','js','C','js','python','C','js','python','css','js','html','node' ]
In [8]: count_d =  {}
In [9]: for i in arr:
   ...:     if i not in count_d:
   ...:         count_d[i] = 1
   ...:     else :
   ...:         count_d[i]+= 1
In [10]: print count_d
{'node': 1, 'C': 3, 'python': 3, 'js': 6, 'html': 1, 'css': 1}

##小练习:作业
作业:求一个序列最大的两个值。arr = [1,2,3,6,111,32,433,211,10002,4444,222]
#!/usr/bin/python 
arr = [1,2,3,6,111,32,433,211,10002,4444,222]
max1 = 0
max2 = 0

for i in arr:
	if i >= max1:
		max2 = max1
		max1 = i
	elif (i < max1) and (i >= max2):
		max2 = i 
	
print max1, max2


[root@www ~]# python zuoye.py 
10002 4444
[root@www ~]#
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档