2022年4月25日:通过Python构建真实的应用程序 --Python简介

Web编程

数据分析

机器学习

什么是Python?

创建于20世纪90年代初期

用途十分广泛,范围涉及从自动执行重复性任务和编写Web应用到构建机器学习模型和实现神经网络。

运行Python代码

Python是一种解释型语言,无需进行编译。

有两种方式执行Python代码:交互模式和脚本模式。

Python实现

CPython通常用于Web开发、应用程序开发和脚本编辑。

Anaconda:专为科学编程任务(如数据科学和机器学习)量身定制的专业Python发行版。

IronPython:基于.NET运行时构建的Python的开放源代码实现。

Jupyter Notebook:基于Web的交互式编程环境,支持包括Python在内的各种编程语言。Jupyter笔记本在研究和学术领域得到广泛运用,用于数学建模、机器学习、统计分析以及编码的教学与学习。

借助Python使用Azure Cloud Shell进行开发。

使用REPL

可以输入较短的语句并对这些语句进行计算。

Python REPL

Python支持交互式控制台体验。“读取-求值-输出-循环”

REPL功能

使用REPL可执行大多数能在代码文件中执行的操作:

运行并计算语句。

声明变量和函数

使用内置帮助。

使用帮助

REPL有一个可用于查找关键字和函数的内置帮助函数。此函数的通用语法是:

help([object])

交互式帮助控制台

如果不将参数传递到帮助函数,则会启动交互式帮助工具。

键入help()以进入交互式帮助控制台。

>>> help()

Welcome to Python 3.10's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the internet at https://docs.python.org/3.10/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> string
Help on module string:

NAME
    string - A collection of string constants.

MODULE REFERENCE
    https://docs.python.org/3.10/library/string.html

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    Public module variables:

    whitespace -- a string containing all ASCII whitespace
    ascii_lowercase -- a string containing all ASCII lowercase letters
    ascii_uppercase -- a string containing all ASCII uppercase letters
    ascii_letters -- a string containing all ASCII letters
    digits -- a string containing all ASCII decimal digits
    hexdigits -- a string containing all ASCII hexadecimal digits
    octdigits -- a string containing all ASCII octal digits
    punctuation -- a string containing all ASCII punctuation characters
    printable -- a string containing all ASCII characters considered printable

CLASSES
    builtins.object
        Formatter
        Template

    class Formatter(builtins.object)
     |  Methods defined here:
     |
     |  check_unused_args(self, used_args, args, kwargs)
     |
     |  convert_field(self, value, conversion)
     |
     |  format(self, format_string, /, *args, **kwargs)
     |
     |  format_field(self, value, format_spec)
     |
     |  get_field(self, field_name, args, kwargs)
     |      # given a field_name, find the object it references.
     |      #  field_name:   the field being looked up, e.g. "0.name"
     |      #                 or "lookup[3]"
     |      #  used_args:    a set of which args have been used
     |      #  args, kwargs: as passed in to vformat
     |
     |  get_value(self, key, args, kwargs)
     |
     |  parse(self, format_string)
     |      # returns an iterable that contains tuples of the form:
     |      # (literal_text, field_name, format_spec, conversion)
     |      # literal_text can be zero length
     |      # field_name can be None, in which case there's no
     |      #  object to format and output
     |      # if field_name is not None, it is looked up, formatted
     |      #  with format_spec and conversion and then used
     |
     |  vformat(self, format_string, args, kwargs)
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)

    class Template(builtins.object)
     |  Template(template)
     |
     |  A string class for supporting $-substitutions.
     |
     |  Methods defined here:
     |
     |  __init__(self, template)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |
     |  safe_substitute(self, mapping={}, /, **kws)
     |
     |  substitute(self, mapping={}, /, **kws)
     |
     |  ----------------------------------------------------------------------
     |  Class methods defined here:
     |
     |  __init_subclass__() from builtins.type
     |      This method is called when a class is subclassed.
     |
     |      The default implementation does nothing. It may be
     |      overridden to extend subclasses.
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |
     |  braceidpattern = None
     |
     |  delimiter = '$'
     |
     |  flags = re.IGNORECASE
     |
     |  idpattern = '(?a:[_a-z][_a-z0-9]*)'
     |
     |  pattern = re.compile('\n            \\$(?:\n              ...identifie...

FUNCTIONS
    capwords(s, sep=None)
        capwords(s [,sep]) -> string

        Split the argument into words using split, capitalize each
        word using capitalize, and join the capitalized words using
        join.  If the optional second argument sep is absent or None,
        runs of whitespace characters are replaced by a single space
        and leading and trailing whitespace are removed, otherwise
        sep is used to split and join the words.

DATA
    __all__ = ['ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'cap...
    ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    digits = '0123456789'
    hexdigits = '0123456789abcdefABCDEF'
    octdigits = '01234567'
    printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
    punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    whitespace = ' \t\n\r\x0b\x0c'

FILE
    c:\users\a-xiaobodou\appdata\local\programs\python\python310\lib\string.py


help>

You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.
>>>

Python中的变量和基本数据类型

变量是使用Python编写的程序 的基本构建基块之一。

变量

在 Python 中,声明变量并使用赋值运算符 = 为其赋值。 被赋值的变量在运算符左侧,赋予的值在右侧。

Python是动态类型化语言。

使用数字

Python 根据名为 int 的内置数据类型创建整数,并将小数(浮点数)作为 float 的实例。 Python 的内置 type() 函数返回变量的数据类型。

使用布尔值

布尔类型包括有True或False。

 从技术角度来说,True 具有值 1,False 具有值 0。 通常,布尔值不用于执行数学运算;相反,它们用于做出决策并执行分支。

使用字符串

除了数字,字符串也是最常用的数据类型。 字符串是零个或多个字符的集合。 通常使用单引号声明字符串,但也可以使用双引号。

在控制台中显示 

print 函数就是其中之一,用于将文本输出到屏幕。

传递到 print 的参数是字符串,这是 Python 中用于存储和管理文本的基础数据类型之一。

读取键盘输入

用户输入

为了读取键盘输入,Python 提供了 input() 函数。 input() 可读取用户在键盘上键入的内容并将其作为字符串返回。

读取数字作为输入

input 函数始终以字符串(文本)的形式返回键入值。

将数字转换为字符串

str() 方法将获取整数或浮点值并将其转换为字符串。

生成计算器

前两个语句末尾的符号 ;\ 表示存在许多代码行,可以一行一行地输入所有代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值