【Python学习】 之 Python3.x(小知识点汇集)

(1)

这里写图片描述

(2)

这里写图片描述

(3)

这里写图片描述

(4)

这里写图片描述

(5)输入函数

这里写图片描述

(6)字符串操作

这里写图片描述

(7)表达式

这里写图片描述

(8)分支语句

这里写图片描述

(9)赋值语句

这里写图片描述

(10)输出

这里写图片描述

(11)循环语句

这里写图片描述

(12)数据类型总图

(13)数字类型

  ①整数类型

这里写图片描述

  ②浮点数类型

这里写图片描述
这里写图片描述

  ③复数类型

这里写图片描述

(14)数字类型转换

这里写图片描述

(15)数字类型运算

这里写图片描述

(16)字符串类型

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

(17)元组类型

这里写图片描述
这里写图片描述
这里写图片描述

(18)列表类型

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

(19)math库使用

这里写图片描述
这里写图片描述

(20)random库使用

这里写图片描述
这里写图片描述
这里写图片描述

(21)函数定义

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

(22)文件操作

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

(23)字典

收集统计文件中的单词并画柱状图

这里写图片描述

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 25 23:02:43 2017

@author: Donald
"""

import turtle

##全局变量##
#词频排列显示个数
count = 10
#单词频率数组-作为y轴数据
data = []
#单词数组-作为x轴数据
words = []
#y轴显示放大倍数-可以根据词频数量进行调节
yScale = 6
#x轴显示放大倍数-可以根据count数量进行调节
xScale = 30

################# Turtle Start  ####################  
#从点(x1,y1)到(x2,y2)绘制线段
def drawLine(t, x1, y1, x2, y2):
    t.penup()
    t.goto (x1, y1)
    t.pendown()
    t.goto (x2, y2)

# 在坐标(x,y)处写文字
def drawText(t, x, y, text):
    t.penup()
    t.goto (x, y)
    t.pendown()
    t.write(text)

def drawGraph(t):
    #绘制x/y轴线
    drawLine (t, 0, 0, 360, 0)
    drawLine (t, 0, 300, 0, 0)

    #x轴: 坐标及描述
    for x in range(count):
        x=x+1 #向右移一位,为了不画在原点上
        drawText(t, x*xScale-4, -20, (words[x-1]))
        drawText(t, x*xScale-4, data[x-1]*yScale+10, data[x-1])
    drawBar(t)

#绘制一个柱体
def drawRectangle(t, x, y):
    x = x*xScale
    y = y*yScale#放大倍数显示
    drawLine(t, x-5, 0, x-5, y)
    drawLine(t, x-5, y, x+5, y)
    drawLine(t, x+5, y, x+5, 0)
    drawLine(t, x+5, 0, x-5, 0)

#绘制多个柱体
def drawBar(t):
    for i in range(count):
        drawRectangle(t, i+1, data[i])    
################# Turtle End  ####################


#对文本的每一行计算词频的函数
def processLine(line, wordCounts):
    #用空格替换标点符号
    line = replacePunctuations(line)
    #从每一行获取每个词
    words = line.split() 
    for word in words:
        if word in wordCounts:
            wordCounts[word] += 1
        else:
            wordCounts[word] = 1

#空格替换标点的函数
def replacePunctuations(line):
    for ch in line:
        if ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":
            line = line.replace(ch, " ")
    return line

def main():
    #用户输入一个文件名
    filename = input("enter a filename:").strip()
    infile = open(filename, "r")

    #建立用于计算词频的空字典
    wordCounts = {}
    for line in infile:
        processLine(line.lower(), wordCounts)

    #从字典中获取数据对
    pairs = list(wordCounts.items())

    #列表中的数据对交换位置,数据对排序
    items = [[x,y]for (y,x)in pairs] 
    items.sort() 

    #输出count个数词频结果
    for i in range(len(items)-1, len(items)-count-1, -1):
        print(items[i][1]+"\t"+str(items[i][0]))
        data.append(items[i][0])
        words.append(items[i][1])

    infile.close()

    #根据词频结果绘制柱状图
    turtle.title('词频结果柱状图')
    turtle.setup(900, 750, 0, 0)
    t = turtle.Turtle()
    t.hideturtle()
    t.width(3)
    drawGraph(t)

#调用main()函数
if __name__ == '__main__':
    main()

(24)面向对象编程

# -*- coding: utf-8 -*-
"""
Created on Thu Jan 26 20:14:30 2017

@author: Donald
"""

from math import sin, cos, radians

class Projectile:
    def __init__(self, angle, velocity, height):
        #根据给定的发射角度、初始速度和位置创建一个投射体对象
        self.xpos = 0.0
        self.ypos = height
        theta = radians(angle)
        self.xvel = velocity * cos(theta)
        self.yvel = velocity * sin(theta)

    def update(self, time):
        #更新投射体的状态
        self.xpos = self.xpos + time * self.xvel
        yvell = self.yvel - 9.8 * time
        self.ypos = self.ypos + time * (self.yvel + yvell) / 2.0
        self.yvel = yvell

    def getY(self):
        #返回投射体的角度
        return self.ypos

    def getX(self):
        #返回投射体的距离
        return self.xpos
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 26 20:16:34 2017

@author: Donald
"""


from Projectile import *

def getInputs():
    a = eval(input("Enter the launch angle (in degrees):"))
    v = eval(input("Enter the initial velocity (in meters/sec):"))
    h = eval(input("Enter the initial height (in meters):"))
    t = eval(input("Enter the time interval: "))
    return a,v,h,t

def main():
    angle,vel,h0,time = getInputs()
    shot = Projectile(angle,vel,h0)
    while shot.getY() >=0:
        shot.update(time)
    print("\nDistance traveled:{0:0.1f}meters.".format(shot.getX()))

if __name__ == "__main__":
    main()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python 2.x和3.x在某些方面不兼容,其中一些主要差异包括但不限于以下几点: 1. 语法问题。 Python 3.x不允许使用print语句,使用print()函数代替。另外,Python 3.x使用unicode字符串作为默认字符串类型,而Python 2.x默认使用ASCII字符串。 2. 数据类型问题。 Python 3.x中有一些数据类型的命名有所改变,例如:long在Python 3.x中被替换为int。另外,Python 2.x中的整型是可以自动转换为浮点型的,但是Python 3.x进行了更严格的分离,并放弃了兼容性。 3. 标准库问题。 Python标准库也不同,某些在Python 2.x中存在的模块和函数在Python 3.x中被移除。例如,Python 3.x中的urllib库和Python 2.x中的urllib2和urllib库有所区别。 4. 其他问题。 Python 3.x有更好的Unicode支持,在处理国际化问题时更加方便。而Python 2.x还是使用基于ASCII的字节串,对Unicode字符的处理可能会产生编码错误。Python 3.x中的一些函数返回的对象类型也有所变化。例如filter、map、range、zip等函数,返回的对象都是生成器类型,而在Python 2.x中返回的是列表。 因此,如果你从Python 2.x迁移到Python 3.x,需要仔细阅读文档,认真处理代码兼容性和数据类型问题。 ### 回答2: Python 2.x和3.x是两个不兼容的版本,主要表现在以下方面: 1. 编码:在Python 2.x中,字符串默认使用ASCII编码,而在Python 3.x中,字符串默认使用Unicode编码。因此,当我们在Python 3.x中运行旧的Python 2.x代码时,需要修改代码以支持Unicode编码。 2. print函数:在Python 2.x中,print函数是一个语句,而在Python 3.x中,它是一个函数。因此,在Python 3.x中,请使用括号来包含打印的内容,例如:print("hello world")。 3. 整除运算符:在Python 2.x中,使用单斜杠(/)进行整除运算,结果为整数,而在Python 3.x中,使用两个斜杠(//)进行整除运算,结果为浮点数。 4. xrange函数和range函数:在Python 2.x中,我们使用xrange函数生成一个迭代器对象,而在Python 3.x中,xrange函数已经被删除了,而range函数直接返回一个迭代器对象。 5. 异常:在Python 2.x中,异常类型可以是字符串或实例对象,而在Python 3.x中,只能是实例对象。因此,在Python 3.x中,请使用try-except语句来处理异常,而不是try-except语句。 6. input函数和raw_input函数:在Python 2.x中,我们使用raw_input函数来获取用户输入,而在Python 3.x中,这个函数已经被删除了,它被替换为input函数。 总结起来,Python 2.x和3.x之间的不兼容主要表现在编码、print函数、整除运算符、xrange函数和range函数、异常以及input函数和raw_input函数等方面。因此,我们需要认真学习并理解这些不兼容的内容,以便在编码时避免出现问题。 ### 回答3: Python是一种广受欢迎的高级编程语言,提供了许多丰富的编程工具和库。但是,Python在2.x和3.x版本之间有很多不兼容之处,这对于那些习惯于Python 2.x的开发人员来说可能是一个问题。 最重要的不兼容之处是语言的语法改变。Python 3.x的语法与之前版本有很大不同,比如print语句就有了一个参数。在Python 2中,它可以像这样使用: ``` print "Hello, world!" ``` 但是在Python 3中,你必须使用括号: ``` print("Hello, world!") ``` Python 3.x还引入了新的关键字,比如async和await。这些关键字在Python 2.x中并不存在,并且如果尝试在Python 2中使用这些关键字,会导致语法错误。 Python 3.x还有一个重要的不兼容点是对Unicode的处理方式。在Python 2.x中,字符串被表示为8位ASCII字符,但在Python 3.x中,字符串被表示为Unicode字符。这意味着在Python 2.x中可以使用类似“str”和“unicode”的函数来处理字符串,但在Python 3.x中只能使用一个“str”函数来处理所有类型的字符串。 此外,Python 3.x中还删除了一些在之前版本中广泛使用的模块和函数。例如,许多与旧版本的“tkinter”库相关的函数被删除,而在Python 2.x中使用的“file”函数也被删除。 综上所述,Python 2.x和3.x之间有很多不兼容之处。对于那些从Python 2.x迁移到Python 3.x的开发人员来说,这些不兼容问题需要花费一些时间和精力来解决。但是,Python 3.x引入了很多新的特性和功能,是新项目的绝佳选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值