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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

常用的jdk下载地址

jdk下载地址 安装方式可以看之前的博客: mac安装jdk oracle 版本:https://www.oracle.com/java/technologies/downloads/ Eclipse Temurin版本:https://adoptium.net/zh-CN/temurin/releases/ 阿里版本: github:https://github.com/

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]