chapter1:python 基础(数据类型,运算符,常用内置函数,模型,strings等)

本文主要是介绍chapter1:python 基础(数据类型,运算符,常用内置函数,模型,strings等),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一章:python基础

 

1 运算符

 

>>1+2*3

>>7

 

整除

>>1/2

>>0

 

浮点数除,任何一方浮点即可

>>1/2.0

>>0.5

 

取余

>>7%5

>>2

 

求幂

>>2**3

>>8

 

注意:幂结合度比负号大

>>-2**2

>>-4

 

扩充:__future__用于导入python未来支持的语言特征

>>> from __future__ import division

>>> 1/2

0.5

>>> 1//2

0

一些future的特性:

feature

optional in

mandatory in

effect

nested_scopes

2.1.0b1

2.2

PEP 227Statically Nested Scopes

generators

2.2.0a1

2.3

PEP 255Simple Generators

division

2.2.0a2

3.0

PEP 238Changing the Division Operator

absolute_import

2.5.0a1

2.7

PEP 328Imports: Multi-Line and Absolute/Relative

with_statement

2.5.0a1

2.6

PEP 343The “with” Statement

print_function

2.6.0a2

3.0

PEP 3105Make print a function

unicode_literals

2.6.0a2

3.0

PEP 3112Bytes literals in Python 3000

 

2 长整型

1)     2.2版本后,会自动检测长整型

>>100000000000000000

    100000000000000000l

2)     log和int结合会自动转换为long,python中会自动转换

3)     各数据类型的范围

3  十六进制 & 八进制

>>0xAF  >>175

>>010    >>8

备注:A= 10     F = 15  ->  10*16^1+ 15*16^0 ->175

4 表达式和语句

表达式是运算的过程,语句如赋值,print,不过过于纠结二者的差别

3.0中print为函数,print(42)即可打印出42

5 输入input()

>>input()

‘will print out’

>>will print out

>>x = input(‘this is a prompt : ’)

This is a prompt : 40

>>print x

>>40

5 build-in functions

说明:更多build-infunctions 可查看参考文档

ads(-10) 10 ,求模

pow(2,3) 8,求幂

round(1.2)  float型的四舍五入

6 modules - > math

>>import math

>>math.floor(3.8) 3,取靠近小的整数

>>math.ceil(3,1) 4,去靠近大的整数

>>from math import sqrt   from … import …方式引入,不用加前缀

>>sqrt(9) 3.0 

>>fun1 = math.sqrt

>>fun1(9) 相等于 sqrt(9)

 

7 modules - > cmath ,可用于处理负数

注意:cmath不能用from …import …

>>import cmath

>> cmath .sqrt(-9)

8 #!让py直接执行

#!/usr/bin/python, Linux下在py文件首行加#!python执行文件的绝对路径;

chmod a+x hello.py ,给所有用户加可执行权限

hello.py 或者./hello.py即可,不用pythonhello.py

9 windows 下双击

一闪而过,看不清楚执行结果,在最后加上raw_input(“Press<enter>”),可以停留方便看到显示结果

10 Strings-> 单引号’’,双引号””,转移字符\

>>> "hello"

'hello'

>>> 'hello'

'hello'

>>> "let's go"

"let's go"

>>> '"hello"'

'"hello"'

>>> 'lett\'s go'

"lett's go"

11 str & repr & `` - >把python value 转为string

>>> print 'hello'

hello

>>> print '1000l'

1000l

>>> print str('hello')

hello

>>> print repr('hello')

'hello'  展示python原有的格式

>>> print 'string & int ' + `4`

string & int 4     :sting和数字

>>> print `'hello'`

'hello'

12 input & raw_input

说明:尽量使用raw_input

>>> raw_input()

hello

'hello'

>>> input()

hello

Traceback (most recent call last):

  File"<pyshell#17>", line 1, in <module>

    input()

  File"<string>", line 1, in <module>

NameError: name 'hello' is not defined

13 long Strings ,raw Strings, Unicode

Long Strings -> 三重引号

>>> print '''

'line1'

"line2"

line3

'''

---

'line1'

"line2"

line3

Long Strings -> 引号+\

>>print "line1\

    line2"

    line1line2

>>> 1+2\

     +3

6

Raw Strings->\不为转义字符

>>> print 'c:\nowhere'

c:

owhere

>>> print r'c:\nowhere'

c:\nowhere

>>> print r'c:\nowhere\'

SyntaxError: EOL while scanning string literal     #不可\放到最后

Unicode Strings

一般strings存放8-bitASCII,unicodestrings 16-bit Unicode

>>> print u'hello,unicode'

hello,unicode


A Quick Summary from Beginning Python

This chapter covered quite a bit of material. Let’s take a look at what you’ve learned before
moving on.
Algorithms: An algorithm is a recipe telling you exactly how to perform a task. When you
program a computer, you are essentially describing an algorithm in a language the computer
can understand, such as Python. Such a machine-friendly description is called a
program, and it mainly consists of expressions and statements.
Expressions: An expression is a part of a computer program that represents a value. For
example, 2+2 is an expression, representing the value 4. Simple expressions are built from
literal values (such as 2 or "Hello") by using operators (such as + or %) and functions (such
as pow). More complicated expressions can be created by combining simpler expressions
(e.g., (2+2)*(3-1)). Expressions may also contain variables.
Variables: A variable is a name that represents a value. New values may be assigned to
variables through assignments such as x = 2. An assignment is a kind of statement.
Statements: A statement is an instruction that tells the computer to do something. That
may involve changing variables (through assignments), printing things to the screen (such
as print "Hello, world!"), importing modules, or a host of other stuff.
Functions: Functions in Python work just like functions in mathematics: they may take
some arguments, and they return a result. (They may actually do lots of interesting stuff
before returning, as you will find out when you learn to write your own functions in
Chapter 6.)
Modules: Modules are extensions that can be imported into Python to extend its capabilities.
For example, several useful mathematical functions are available in the math module.
Programs: You have looked at the practicalities of writing, saving, and running Python
programs.
Strings: Strings are really simple—they are just pieces of text. And yet there is a lot to know
about them. In this chapter, you’ve seen many ways to write them, and in Chapter 3 you
learn many ways of using them.


New Functions in This Chapter

Function Description
abs(number) Returns the absolute value of a number
cmath.sqrt(number) Returns the square root; works with negative numbers
float(object) Converts a string or number to a floating-point number
help() Offers interactive help
input(prompt) Gets input from the user
int(object) Converts a string or number to an integer
long(object) Converts a string or number to a long integer
math.ceil(number) Returns the ceiling of a number as a float
math.floor(number) Returns the floor of a number as a float
math.sqrt(number) Returns the square root; doesn’t work with negative numbers
pow(x, y[, z]) Returns x to the power of y (modulo z)
raw_input(prompt) Gets input from the user, as a string
repr(object) Returns a string representation of a value
round(number[, ndigits]) Rounds a number to a given precision
str(object) Converts a value to a string


这篇关于chapter1:python 基础(数据类型,运算符,常用内置函数,模型,strings等)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/610084

相关文章

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应