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打造一个可视化FTP服务器

《基于Python打造一个可视化FTP服务器》在日常办公和团队协作中,文件共享是一个不可或缺的需求,所以本文将使用Python+Tkinter+pyftpdlib开发一款可视化FTP服务器,有需要的小... 目录1. 概述2. 功能介绍3. 如何使用4. 代码解析5. 运行效果6.相关源码7. 总结与展望1

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.