
一、运行环境
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 python3.退出命令
退出命令:ctrl+d或者输入 exit() 退出
二、python注意事项
1.pyrhon文件中未指定编码时,在执行过程中会出错
原因:python默认编码格式是ASCII格式,未修改时无法正确打印汉字
解决办法:在文件开头添加 #-*- Coding:UTF-8 -*- 或者 #Coding=utf-8
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"hello2.多行语句显示
使用"\"可以将一行语句多行显示
@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 World2.等待用户输入
@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!TEST4.字典
@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中演示)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)