Python编程入门(Linux中演示)

Python编程入门(Linux中演示),第1张

概述一、运行环境1.确认是否安装python[root@master1 ~]# pythonPython 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2Type "help", "copyright", "credits" or "license" for more information.&g

一、运行环境

1.确认是否安装python

@H_502_4@[root@master1 ~]# pythonPython 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>

2.若没有安装,使用yum安装

@H_502_4@[root@master1 ~]# yum install python

3.退出命令

退出命令:ctrl+d或者输入 exit() 退出

二、python注意事项

1.pyrhon文件中未指定编码时,在执行过程中会出错

原因:python默认编码格式是ASCII格式,未修改时无法正确打印汉字

解决办法:在文件开头添加  #-*- Coding:UTF-8 -*-  或者  #Coding=utf-8

三、python交互式编程实例演示

1.输出hello

@H_502_4@[root@master1 pythonStudy]# pythonPython 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> print "hello"hello

2.多行语句显示

使用"\"可以将一行语句多行显示

@H_502_4@>>> a=1>>> b=2>>> c=3>>> total=a+\... b+\... c>>> total6

语句中包含[] ,{},或者()时,不需要使用多行连接符,如:

@H_502_4@>>> days = ['Monday','Tuesday','Wednesday',... 'Thursday','FrIDay']>>> days['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'FrIDay']

四、python脚本式编程实例演示

1.helloWorld

@H_502_4@[root@master1 pythonStudy]# vim helloWorld.py

添加:

@H_502_4@#!/usr/bin/pythonprint "Hello World";

运行输出:

@H_502_4@[root@master1 pythonStudy]# python helloWorld.py Hello World

2.等待用户输入

@H_502_4@#!/usr/bin/python#Coding=utf-8#功能实现:等待用户输入#文件名:001.pyraw_input("按下enter键退出,其他任意键显示...\n")

运行输出:

@H_502_4@[root@master1 pythonStudy]# python 001.py按下enter键退出,其他任意键显示...[root@master1 pythonStudy]#

3.python字符串练习

@H_502_4@#!/usr/bin/python#-*- Coding:UTF-8 -*-str = 'Hello World!'print str #输出完整字符串print str[0] #输出字符串中的第一个字符print str[2:5]#输出字符串中第三个至第五个之间的字符串print str[2:]#输出从第三个字符开始的字符串print str *2 #打印两次print str + "TEST" #输出连接字符串

运行输出:

@H_502_4@[root@wugenqiang pythonStudy]# python test001.pyHello World!Hllollo World!Hello World!Hello World!Hello World!TEST

4.字典

@H_502_4@#!/usr/bin/python#Coding=utf-8#功能实现:Python字典dict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name':'john','code':6734,'dept':'sales'}print dict['one']       #输出键位‘one’的值print dict[2]           #输出键位2的值print tinydict          #输出完整的字典print tinydict.keys()   #输出所有键print tinydict.values() #输出所有值

运行输出:

@H_502_4@[root@wugenqiang pythonStudy]# python dict01.pyThis is oneThis is two{'dept': 'sales', 'code': 6734, 'name': 'john'}['dept', 'code', 'name']['sales', 6734, 'john']

 

 

总结

以上是内存溢出为你收集整理的Python编程入门(Linux中演示)全部内容,希望文章能够帮你解决Python编程入门(Linux中演示)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1185417.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存