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

相关文章

SQL常用操作精华之复制表、跨库查询、删除重复数据

《SQL常用操作精华之复制表、跨库查询、删除重复数据》:本文主要介绍SQL常用操作精华之复制表、跨库查询、删除重复数据,这些SQL操作涵盖了数据库开发中最常用的技术点,包括表操作、数据查询、数据管... 目录SQL常用操作精华总结表结构与数据操作高级查询技巧SQL常用操作精华总结表结构与数据操作复制表结

Python 异步编程 asyncio简介及基本用法

《Python异步编程asyncio简介及基本用法》asyncio是Python的一个库,用于编写并发代码,使用协程、任务和Futures来处理I/O密集型和高延迟操作,本文给大家介绍Python... 目录1、asyncio是什么IO密集型任务特征2、怎么用1、基本用法2、关键字 async1、async

Python实现剪贴板历史管理器

《Python实现剪贴板历史管理器》在日常工作和编程中,剪贴板是我们使用最频繁的功能之一,本文将介绍如何使用Python和PyQt5开发一个功能强大的剪贴板历史管理器,感兴趣的可以了解下... 目录一、概述:为什么需要剪贴板历史管理二、功能特性全解析2.1 核心功能2.2 增强功能三、效果展示3.1 主界面

Python与Java交互出现乱码的问题解决

《Python与Java交互出现乱码的问题解决》在现代软件开发中,跨语言系统的集成已经成为日常工作的一部分,特别是当Python和Java之间进行交互时,编码问题往往会成为导致数据传输错误、乱码以及难... 目录背景:为什么会出现乱码问题产生的场景解决方案:确保统一的UTF-8编码完整代码示例总结在现代软件

JavaScript时间戳与时间的转化常用方法

《JavaScript时间戳与时间的转化常用方法》在JavaScript中,时间戳(Timestamp)通常指Unix时间戳,即从1970年1月1日00:00:00UTC到某个时间点经过的毫秒数,下面... 目录1. 获取当前时间戳2. 时间戳 → 时间对象3. 时间戳php → 格式化字符串4. 时间字符

Python+Tkinter实现Windows Hosts文件编辑管理工具

《Python+Tkinter实现WindowsHosts文件编辑管理工具》在日常开发和网络调试或科学上网场景中,Hosts文件修改是每个开发者都绕不开的必修课,本文将完整解析一个基于Python... 目录一、前言:为什么我们需要专业的Hosts管理工具二、工具核心功能全景图2.1 基础功能模块2.2 进

Python多重继承慎用的地方

《Python多重继承慎用的地方》多重继承也可能导致一些问题,本文主要介绍了Python多重继承慎用的地方,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录前言多重继承要慎用Mixin模式最后前言在python中,多重继承是一种强大的功能,它允许一个

python+OpenCV反投影图像的实现示例详解

《python+OpenCV反投影图像的实现示例详解》:本文主要介绍python+OpenCV反投影图像的实现示例详解,本文通过实例代码图文并茂的形式给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前言二、什么是反投影图像三、反投影图像的概念四、反向投影的工作原理一、利用反向投影backproj

MySQL复合查询从基础到多表关联与高级技巧全解析

《MySQL复合查询从基础到多表关联与高级技巧全解析》本文主要讲解了在MySQL中的复合查询,下面是关于本文章所需要数据的建表语句,感兴趣的朋友跟随小编一起看看吧... 目录前言:1.基本查询回顾:1.1.查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J1.2.按照部门

Python中edge-tts实现便捷语音合成

《Python中edge-tts实现便捷语音合成》edge-tts是一个功能强大的Python库,支持多种语言和声音选项,本文主要介绍了Python中edge-tts实现便捷语音合成,具有一定的参考价... 目录安装与环境设置文本转语音查找音色更改语音参数生成音频与字幕总结edge-tts 是一个功能强大的