iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python开发(一)
  • 433
分享到

Python开发(一)

Python 2023-01-31 02:01:07 433人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

python>>> s='tou siheoiw' >>> '%s is number %d' % (s[:6],1) 'tou si is number 1'>>> hi='''hi

python

>>> s='tou siheoiw'
>>> '%s is number %d' % (s[:6],1)
'tou si is number 1'


>>> hi='''hi
there'''
>>> hi
'hi\nthere'


>>> book ={'title':'Python WEB Development','year':2008}
>>> book
{'year': 2008, 'title': 'Python Web Development'}
>>> 'year' in book
True
>>> 'pub' in book
False


setdefault和get一样,dict.get(key)或是dict[key]

>>> d ={'title':'Python Web Development','year':2008}
>>> d
{'year': 2008, 'title': 'Python Web Development'}
>>> d.setdefault('pub','Addison Wesley')
'Addison Wesley'
>>> d
{'year': 2008, 'pub': 'Addison Wesley', 'title': 'Python Web Development'}
>>> del d['pub']
>>> d
{'year': 2008, 'title': 'Python Web Development'}
>>> d['title']
'Python Web Development'
>>> len(d)
2


while循环:

>>> while i<5:
...     print i
...     i +=1
...    
... 
0
1
2
3
4

建一个文本

#!/bin/bash
#4.4.sh
i=$[ $1 % 2]
if test $i -eq  0 ; then
   echo oushu
else
   echo jishu
fi
~
>>> for line in open('4.sh'):
...     if 'jishu' in line:  
...         print line
... 
   echo jishu
>>> for line in open('4.sh'):
...     print line
... 
#!/bin/bash

#4.4.sh

i=$[ $1 % 2]

if test $i -eq  0 ; then

   echo oushu

else 

   echo jishu

fi

enumerate是一个能让你同时迭代和计数的内置函数

>>> data =(123,'abc',3.14)
>>> for i, value in enumerate(data):
...     print i,value
... 
0 123
1 abc
2 3.14

简单的计算

#!/usr/bin/python
#filename:expression.py
length=5
breadth=2
area=length*breadth
print 'Area is',area
print 'Perimeter is',2*(length+breadth)
                                                                            
"expression.py" [New] 7L, 142C written

# python expression.py
Area is 10
Perimeter is 14

 

输入转化为×××:int(raw_input(''))

#!/usr/bin/python
#Filename:if.py
number=23
guess=int(raw_input('Enter an integer:'))
if guess==number:
    print 'Congratulations,u guessed it.'
    print "(but u do not w in any prizes!)"
elif guess< number:
    print 'No ,it is a little higher than that'
else:
    print 'No,it is a little lower than that'
                                                                           
~                                                                               
"if.py" [New] 12L, 311C written
# python if.py
Enter an integer:78
No,it is a little lower than that

continue:

 有关计算字符串长度len(s)

#!/usr/bin/python
#Filename:continue.py
while True:
    s=raw_input('Enter something:')
    if s=='quit':
        break
    if len(s)<3:
        continue
    print 'Input is of sufficient length'
~                                                                               
                                                                             
~                                                                               
"continue.py" 9L, 196C written                                
# python continue.py
Enter something:77
Enter something:e
Enter something:3
Enter something:eee
Input is of sufficient length
Enter something:
Enter something:quit
#

定义函数

def 函数名():

函数体


函数名()看见没那么快,

 #!/usr/bin/python
#filename:func_param.py
def printMax(a,b):
    if a>b:
        print a,'is maximum'
    else:
        print b,'is maximum'
printMax(3,4)

                                                                            
~                                                                               
"func_param.py" [New] 9L, 156C written                        

# python func_param.py
4 is maximum

局部变量:

#!/usr/bin/python
#filename:func_local.py
def func(x):
    print 'x is',x
    x=2
    print 'Changed localx to',x

x=50 
func(x)
print 'x is still',x
~                                                                               
                                                                             
"func_local.py" 10L, 152C written                             
# python func_local.py
x is 50
Changed localx to 2
x is still 50

全局变量:

#!/usr/bin/python
#Filename:func_global.py
def func():
    global x
    print 'x is',x
    x=2
    print 'Changed local x to',x
x=50
func()
print 'Value of x is',x
                                                                            
~                                                                               
"func_global.py" [New] 10L, 164C written                      
# python func_global.py
x is 50
Changed local x to 2
Value of x is 2


定义函数的默认参数:

#!/usr/bin/python
#Filename:func_default.py
def say(message,times=1):
    print message*times
say('Hello')
say('World',5)
~                                                                               
                                                                            
"func_default.py" [New] 6L, 122C written                      
# python func_default.py
Hello
WorldWorldWorldWorldWorld


关键参数:

#!/usr/bin/python
#filename:func_key.py
def func(a,b=5,c=10):
    print 'a is',a,'and b is',b,'and c is',c

func(3,7)
func(25,c=24)
func(c=50,a=100)
~                                                                               
                                                                             
"func_key.py" [New] 8L, 149C written                          
# python func_key.py
a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

return语句

#!/usr/bin/python
def returnn(a,b):
    if a>b:
        return a
    else:
        return b
print returnn(2,4)
~                                                                                                                                                             
~                                                                               
"return.py" 7L, 111C written                                  
# python return.py 
4
def printMax(x,y):
#!/usr/bin/python
def printMax(x,y):
    x=int(x)#convert to integers,if possible
    y=int(y)

    if x>y:
        print x,'is maximum'
    else:
        print y,'is maximum'
printMax(3,5)
print printMax.__doc__ 
                                                                              
~                                                                               
"func_doc.py" 11L, 214C written
# python func_doc.py
5 is maximum
None

sys模块:

模块是包含了你定义的所有的函数和变量的文件


#!/usr/bin/python
#Filename:using_sys.py
import sys
print 'The com m and line arguments are:'
for i in sys.argv:
    print i
print '\n',sys.path,'\n'
~
# python using_sys.py we are arguments
The com m and line arguments are:
using_sys.py
we
are
arguments

['/root',
 '/usr/lib64/python26.zip', '/usr/lib64/python2.6', 
'/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', 
'/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', 
'/usr/lib64/python2.6/site-packages', 
'/usr/lib/python2.6/site-packages']

其中:using_sys.py 是sys.argv[0]

      we 是 sys.argv[1]

      are 是sys.argv[2]

     arguments是sys.argv[3]

字节编译的.pyc文件


模块的__name__


#!/usr/bin/python
#filename 
if __name__=='__main__':
    print 'This program is being run by itself'
else:
    print 'I am being imported from another module'
                                                                             
~                                                                               
"using_name.py" [New] 7L, 161C written                        
[root@10-8-11-204 ~]# python using_name.py
This program is being run by itself
[root@10-8-11-204 ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more infORMation.
>>> import using_name
I am being imported from another module

创建字节的模块


#!/usr/bin/python
import mymodule
mymodule.sayhi()
print 'Version',mymodule.version
~                                                                                                                                                             
"mymodule_demo.py" [New] 4L, 84C written                      
[root@ ~]# python mymodule_demo.py
Hi,this is mymodule speaking
Version 0.1

from...import

from mymodule import sayhi,version

(sayhi,version是mymodule的方法和变量)

#!/usr/bin/python
#filename
from mymodule import sayhi,version
sayhi()
print 'Version',version
~                                                                             
"mymodule_demo2.py" [New] 5L, 95C written
[root@ ~]# python mymodule_demo2.py
Hi,this is mymodule speaking
Version 0.1

dir()函数

[root@ ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> dir(sys) 
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'hexversion', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions']

。未完待续

--结束END--

本文标题: Python开发(一)

本文链接: https://www.lsjlt.com/news/185221.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python开发(一)
    python>>> s='tou siheoiw' >>> '%s is number %d' % (s[:6],1) 'tou si is number 1'>>> hi='''hi ...
    99+
    2023-01-31
    Python
  • Python开发小知识集(一)
    1.在使用Beautifulsoup的,出现错误提示WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHA...
    99+
    2023-01-31
    小知识 Python
  • Python-第一章(开发基础)
    1.    机器语言 = 机器指令 = 二进制代码   汇编语言就是把二进制变成了英文,开发效率低。   编译型语言:C   C++   Delphi  。。。   解译型语言:Python  php   java 。。。     好处:...
    99+
    2023-01-31
    基础 Python
  • Python游戏引擎开发(一):序
    写了这么久的html5,感觉html5学得差不多了,是时候去接触更多的语言来扩充自己的能力了。我先后看了Swift,Java等语言。首先开发Swift需要一台mac,对于我这个寒士而言,过于奢华了一些;Java吧,又感觉太胖了,...
    99+
    2023-01-31
    引擎 游戏 Python
  • 利用 Python 开发一个 Python 解释器
    目录1.标记(Token)2.词法分析器(Lexer)3.巴科斯-诺尔范式(Backus-Naur Form,BNF)4.解析器(Parser)前言: 计算机只能理解机器码。归根结底...
    99+
    2024-04-02
  • Python界面开发:(一)环境搭建
        1、python2.7    2、PyQt4    3、Pycharm(IDE)    1、下载python2.7地址:https://www.python.org/downloads/windows/    2、下载sip   ...
    99+
    2023-01-31
    界面 环境 Python
  • 如何利用Python开发一个Python解释器
    本篇文章给大家分享的是有关如何利用Python开发一个Python解释器,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。前言:计算机只能理解机器码。归根结底,编程语言只是一串文字...
    99+
    2023-06-22
  • 我的第一个python web开发框架(
      前面ORM模块我们已经完成了开发,接下来要做的就是对项目代码进行重构了。因为对底层数据库操作模块(db_helper.py)进行了改造,之前项目的接口代码全都跑不起来了。   在写ORM模块时,我们已经对产品接口的分页查询、新增、修改...
    99+
    2023-01-30
    第一个 框架 python
  • Python中怎么开发一个Windows程序
    今天就跟大家聊聊有关Python中怎么开发一个Windows程序,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。安装py2exe, 这是windows平台下一个非常好的python程序...
    99+
    2023-06-17
  • Python web开发:6本web开发
    Python作为一种灵活好学的脚本语言,已经越来越受程序员的欢迎和热捧,甚至成为程序员的必备技能。Python的Web开放框架如Django,Flask,更是得到了广大的应用,今天为大家推荐几本有关python web开发的书籍。 1.《...
    99+
    2023-01-31
    Python web
  • macos 使用vscode 开发python 爬虫(安装一)
    使用VS Code进行Python爬虫开发是一种常见的选择,下面是一些步骤和建议: 安装VS Code:首先,确保你已经在你的macOS上安装了VS Code。你可以从官方网站(https://cod...
    99+
    2023-10-19
    macos vscode python
  • 基于Python+Pyqt5开发一个应用程序
    介绍你的那个她/他 1. UI —MainWindow设计界面及代码 # -*- coding: utf-8 -*- # Form implementation genera...
    99+
    2024-04-02
  • Python和JavaScript:哪一个更适合Laravel开发?
    Laravel是一个流行的PHP框架,开发者可以使用它来构建高质量的Web应用程序。但是,Laravel的开发并不仅限于PHP语言,开发者还可以使用其他语言来开发Laravel应用程序。目前,Python和JavaScript是最受欢迎的...
    99+
    2023-11-10
    javascript ide laravel
  • python web开发
    HTTP超文本传输协议CSS层叠样式HTML 超文本标记语言JavaScript脚本语言WSGI接口:Web Server Gateway Interface. -- 它只要求web开发者实现一个函数,就可以相应http请求。def app...
    99+
    2023-01-31
    python web
  • 网站开发(周一):项目开发环境
    前言:网站开发教程是在MacBook Pro-macOS Mojave 10.14.2操作系统下,使用Python语言和DjangoMVC架构,开发工具为PyCharm Professional Edition 2018.3,后台服务器为...
    99+
    2023-01-30
    网站开发 环境 项目
  • 利用Python开发一个自动答题程序
    目录环境使用模块使用自动答题思路步骤代码展示环境使用 Python 3.8 –> 解释器 <执行python代码> Pycharm –>...
    99+
    2023-02-03
    Python自动答题程序 Python自动答题 Python 答题
  • Python游戏服务器开发日记(一)目标
            到了新的环境,老大让我有空研究下一代服务器技术,作为一个长期任务。        新的服务器想达到的目标:        1、分布式系统,对象(Entity)之间的关系类似于Actor模型。        2、逻辑服务,...
    99+
    2023-01-31
    目标 服务器 日记
  • numpy和django:哪一个更适合Python Web开发?
    Python是一种非常流行的编程语言,它因其易学易用的特性而备受欢迎。Python在Web开发中也很常见,有许多框架可供选择。在本文中,我们将比较两个流行的Python库:NumPy和Django,并探讨它们在Web开发中的应用。 NumP...
    99+
    2023-11-01
    numpy django numy
  • python版《羊了个羊》游戏开发第一天
    Python小型项目实战教学课《羊了个羊》 一、项目开发大纲(初级) 版本1.0:基本开发 课次 内容 技术 第一天 基本游戏地图数据 面向过程 第二天 鼠标点击和移动 面向对象 第三天 消除 设计模式:单例模式 ...
    99+
    2023-09-01
    python pygame 开发语言
  • Python移动APP开发之Kivy(二)——第一个APP
    目录 一、APP代码书写 (一)单独在py中构建 (二)结合kv构建 二、Kivy打包 (一)、大致概述 (二)、Kivy打包Android的方式 (三)、开始打包 (四)、buildozer.spec详解 三、结语 之前简要介绍了Py...
    99+
    2023-08-31
    python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作