百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

python基础篇之基本概念(python基础讲解)

off999 2024-11-15 23:10 13 浏览 0 评论

python基础篇是以Google for Education上面的Google's Python Class课程[1]为主.这是一门非常适合初学者的课程.

英文介绍

Welcome to Google's Python Class -- this is a free class for people with a little bit of programming experience who want to learn Python. The class includes written materials, lecture videos, and lots of code exercises to practice Python coding. These materials are used within Google to introduce Python to people who have just a little programming experience. The first exercises work on basic Python concepts like strings and lists, building up to the later exercises which are full programs dealing with text files, processes, and http connections. The class is geared for people who have a little bit of programming experience in some language, enough to know what a "variable" or "if statement" is. Beyond that, you do not need to be an expert programmer to use this material.

To get started, the Python sections are linked at the left -- Python Set Up to get Python installed on your machine, Python Introduction for an introduction to the language, and then Python Strings starts the coding material, leading to the first exercise. The end of each written section includes a link to the code exercise for that section's material. The lecture videos parallel the written materials, introducing Python, then strings, then first exercises, and so on. At Google, all this material makes up an intensive 2-day class, so the videos are organized as the day-1 and day-2 sections.

This material was created by Nick Parlante working in the engEDU group at Google. Special thanks for the help from my Google colleagues John Cox, Steve Glassman, Piotr Kaminksi, and Antoine Picard. And finally thanks to Google and my director Maggie Johnson for the enlightened generosity to put these materials out on the internet for free under the under the Creative Commons Attribution 2.5 license -- share and enjoy!

课程结构

该课程主要包含以下三大模块,当然最重要的莫过于课程后面的练习了.

1.文档

2.视频

3.练习

简单python样例

就像刚刚学习c语言接触第一个"Hello world!"程序一样,下面就是一个简单的python版的"Hello world!"程序.

? 代码示例

#!/usr/bin/python

# 导入所用模块 -- sys 是常用的模块
import sys

# 代码写在main()里面
def main():
    print 'Hello World!'
    # 命令行的参数在 sys.argv[1], sys.argv[2] ...
    # sys.argv[0] 表示脚本名称

# 调用main()函数来启动程序的样板
if __name__ == '__main__':
    main()

注意:
代码第一行
#!/usr/bin/python是当使用./scrpt.py执行该段程序时指定解释器的路径. 显然这里是linux的路径,windows可以换成#!C:\Python2.7

基本概念

python里面有一些基本的概念,像模块,函数之类的,理解这些基本概念有利于对python程序的理解和编写.

模块

?定义

模块(类比 java 中包里面的类)是一个包含所有你定义的函数和变量的文件,其后缀名是 .py。模块可以被别的程序引入,以使用该模块中的函数等功能。这也是使用 python 标准库的方法。

?代码示例

?自己定义的模块

# coding=utf-8
#!/usr/bin/python
# Filename: mymodule.py

# 模块中定义的函数
def sayhi():   
    print '模块就是这样建造的.'

# 模块中定义的变量
version = '0.1'

?引用自己定义好的模块

# coding=utf-8
#!/usr/bin/python
# Filename: mymodule_demo.py

# 导入所写模块
import mymodule

# 代码写在main()里面
def main():
    # 显示mymodule中定义的函数和变量
    print "mymodule中定义的函数和变量:" + "  ".join(dir(mymodule))

    # 显示当前main函数中定义的函数的变量
    print "main函数中定义的函数和变量:" + "  ".join(dir())

    # 显示__name__的值
    print "main函数中__name__变量的值:" + __name__

# 调用main()函数来启动程序的样板
if __name__ == '__main__':
    main()

    # 显示当前模块中定义的函数的变量
    print "当前模块中定义的函数和变量:" + "  ".join(dir())

    # 显示__name__的值
    print "当前模块中__name__变量的值:" + __name__


函数

?定义

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数定义注意点

1.以def关键字开头,后面接函数名称和参数以及冒号

2.以return [expression]结束函数

3.参数类型有多个

? 必备参数--一般定义的参数

? 命名参数--复制语句作为参数

? 缺省参数--参数有默认值

? 不定长参数--用*标记,类似指针

? 代码示例

# coding=utf-8
#!/usr/bin/python
# Filename: function_demo.py

# 导入所写模块
import sys

# 自定义函数
# 打印输入的参数和本函数名
def printStr( str ):
    # sys._getframe().f_code.co_name 能够获得当前函数的名称
    print sys._getframe().f_code.co_name , ":" , str
    return

# 打印输入的不定长参数和本函数名
def printChangePar( argc, *argv ):
    print sys._getframe().f_code.co_name , ":" , argc
    vars =""
    for var in argv:
        vars = vars + var + " "
        return vars

# 代码写在main()里面
def main():
    # 传入命名参数
    printStr( str = "I am here ." )

    # 传入不定长参数
    print printChangePar(4,"I","am","here",".")


# 调用main()函数来启动程序的样板
if __name__ == '__main__':
    main()

如果该文章对您产生了帮助,或者您对技术文章感兴趣,可以关注微信公众号: 技术茶话会, 能够第一时间收到相关的技术文章,谢谢!

References

[1] Google's Python Class课程: https://developers.google.com/edu/python/?csw=1

相关推荐

Python Flask 容器化应用链路可观测

简介Flask是一个基于Python的轻量级Web应用框架,因其简洁灵活而被称为“微框架”。它提供了Web开发所需的核心功能,如请求处理、路由管理等,但不会强制开发者使用特定的工具或库。...

Python GUI应用开发快速入门(python开发软件教程)

一、GUI开发基础1.主流GUI框架对比表1PythonGUI框架比较框架特点适用场景学习曲线Tkinter内置库,简单小型应用,快速原型平缓PyQt功能强大,商用许可专业级桌面应用陡峭PySi...

【MCP实战】Python构建MCP应用全攻略:从入门到实战!

实战揭秘:Python Toga 打造跨平台 GUI 应用的神奇之旅

在Python的世界里,GUI(图形用户界面)开发工具众多,但要找到一款真正跨平台、易于使用且功能强大的工具并不容易。今天,我们就来深入探讨一下Toga——一款Python原生、操作系统原...

python应用目录规划(python的目录)

Python大型应用目录结构规划(企业级最佳实践)核心原则模块化:按业务功能拆分,高内聚低耦合可扩展性:支持插件机制和动态加载环境隔离:清晰区分开发/测试/生产环境自动化:内置标准化的构建测试部署流...

Python图形化应用开发框架:PyQt开发简介

PyQt概述定义:PyQt是Python绑定Qt框架的工具集,用于开发跨平台GUI应用程序原理:通过Qt的C++库提供底层功能,PyQt使用SIP工具生成Python绑定特点:支持Windows/ma...

[python] 基于PyOD库实现数据异常检测

PyOD是一个全面且易于使用的Python库,专门用于检测多变量数据中的异常点或离群点。异常点是指那些与大多数数据点显著不同的数据,它们可能表示错误、噪声或潜在的有趣现象。无论是处理小规模项目还是大型...

Python、Selenium 和 Allure 进行 UI 自动化测试的简单示例脚本

环境准备确保你已经安装了以下库:SeleniumAllurepytest你可以使用以下命令安装所需库:pipinstallseleniumallure-pytestpytest示例代码下面的代...

LabVIEW 与 Python 融合:打造强大测试系统的利器

在现代测试系统开发领域,LabVIEW和Python各自凭借独特优势占据重要地位。LabVIEW以图形化编程、仪器控制和实时系统开发能力见长;Python则凭借丰富的库资源、简洁语法和强大数...

软件测试进阶之自动化测试——python+appium实例

扼要:1、了解python+appium进行APP的自动化测试实例;2、能根据实例进行实训操作;本课程主要讲述用python+appium对APP进行UI自动化测试的例子。appium支持Androi...

Python openpyxl:读写样式Excel一条龙,测试报表必备!

无论你是测试工程师、数据分析师,还是想批量导出Excel的自动化工作者,只需一个库openpyxl,即可高效搞定Excel的各种需求!为什么选择openpyxl?支持.xlsx格式...

Python + Pytest 测试框架——数据驱动

引言前面已经和大家介绍过Unittest测试框架的数据驱动框架DDT,以及其实现原理。今天和大家分享的是Pytest测试框架的数据驱动,Pytest测试框架的数据驱动是由pytest自...

这款开源测试神器,圆了我玩游戏不用动手的梦想

作者:HelloGitHub-Anthony一天我在公司用手机看游戏直播,同事问我在玩什么游戏?我和他说在看直播,他恍然大悟:原来如此,我还纳闷你玩游戏,咋不用动手呢。。。。一语惊醒梦中人:玩游戏不用...

Python单元测试框架对比(pycharm 单元测试)

一、核心框架对比特性unittest(标准库)pytest(主流第三方)nose2(unittest扩展)doctest(文档测试)安装Python标准库pipinstallpytestp...

利用机器学习,进行人体33个2D姿态检测与评估

前几期的文章,我们分享了人脸468点检测与人手28点检测的代码实现过程,本期我们进行人体姿态的检测与评估通过视频进行人体姿势估计在各种应用中起着至关重要的作用,例如量化体育锻炼,手语识别和全身手势控制...

取消回复欢迎 发表评论: