`
jiangduxi
  • 浏览: 443828 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Python3入门学习

阅读更多
  在学习一门计算机语言的时候,首先接触的入门程序基本上都是"Hello World".最近学习了下Python3.1也将入门级别的程序给以记录下。
引用

  使用python3.1小技巧:在idle里ctrl+n可以打开一个新窗口,输入源码后ctrl+s可以保存,f5运行程序.


Hello World 程序

  print("hello World !")


你好
  s1 = input("Input your name:")
  print("你好 %s:" % s1)


字符和数字
#用内置函数进行转换
  a=2
  b="test"
  c=str(a)+b
  d="1234"
  e=a+int(d)
 print("c is %s, e is %i" %(c,e))


列表
#! /usr/bin/python
# -*- coding: utf8 -*-
#列表类似Javascript的数组,方便易用

#定义元组
word=['a','b','c','d','e','f','g']

#如何通过索引访问元组里的元素
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: ")
print (b) # index 1 and 2 elements of word.
c=word[:2]
print ("c is: ")
print (c) # index 0 and 1 elements of word.
d=word[0:]
print ("d is: ")
print (d) # All elements of word.

#元组可以合并
e=word[:2]+word[2:]
print ("e is: ")
print (e) # All elements of word.
f=word[-1]
print ("f is: ")
print (f) # The last elements of word.
g=word[-4:-2]
print ("g is: ")
print (g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: ")
print (h) # The last two elements.
i=word[:-2]
print ("i is: ")
print (i) # Everything except the last two characters
l=len(word)
print ("Length of word is: "+ str(l))
print ("Adds new element")
word.append('h')
print (word)

#删除元素
del word[0]
print (word)
del word[1:3]
print (word)

引用

知识点:
  1. 列表长度是动态的,可任意添加删除元素.
  2. 用索引可以很方便访问元素,甚至返回一个子列表


字典
x={'a':'aaa','b':'bbb','c':12}
print (x['a'])
print (x['b'])
print (x['c'])

for key in x:
    print ("Key is %s and value is %s" % (key,x[key]))

引用

知识点:
  1. 将他当Java的Map来用即可.


字符串
word="abcdefg"
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: "+b) # index 1 and 2 elements of word.
c=word[:2]
print ("c is: "+c) # index 0 and 1 elements of word.
d=word[0:]
print ("d is: "+d) # All elements of word.
e=word[:2]+word[2:]
print ("e is: "+e) # All elements of word.
f=word[-1]
print ("f is: "+f) # The last elements of word.
g=word[-4:-2]
print ("g is: "+g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: "+h) # The last two elements.
i=word[:-2]
print ("i is: "+i) # Everything except the last two characters
l=len(word)
print ("Length of word is: "+ str(l))


条件和循环语句
x=int(input("Please enter an integer:"))
if x<0:
    x=0
    print ("Negative changed to zero")

elif x==0:
    print ("Zero")

else:
    print ("More")


# Loops List
a = ['cat', 'window', 'defenestrate']
for x in a:
    print (x, len(x))


引用

知识点:
1. 条件和循环语句
2. 如何得到控制台输入


异常处理
#! /usr/bin/python
s=input("Input your age:")
if s =="":
    raise Exception("Input must no be empty.")

try:
    i=int(s)
except Exception as err:
    print(err)
finally: # Clean up action
    print("Goodbye!")


文件处理
#spath="D:/test/1.txt"
f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.
f.write("First line 1.\n")
f.writelines("First line 2.")

f.close()

f=open(spath,"r") # Opens file for reading

for line in f:
    print("每一行的数据是:%s"%line)
f.close()

引用

知识点:
1. open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.
2. 打开文件之后记得关闭


类和继承
class Base:
    def __init__(self):
        self.data = []
    def add(self, x):
        self.data.append(x)
    def addtwice(self, x):
        self.add(x)
        self.add(x)

# Child extends Base
class Child(Base):
    def plus(self,a,b):
        return a+b

oChild =Child()
oChild.add("str1")
print (oChild.data)
print (oChild.plus(2,3))

引用

知识点:
1. self:类似Java的this参数


包机制
每一个.py文件称为一个module,module之间可以互相导入
# a.py
def add_func(a,b):
    return a+b

# b.py
from a import add_func # Also can be : import a

print ("Import add_func from module a")
print ("Result of 1 plus 2 is: ")
print (add_func(1,2))    # If using "import a" , then here should be "a.add_func"

module可以定义在包里面.Python定义包的方式稍微有点古怪,假设我们有一个parent文件夹,该文件夹有一个child子文件夹.child中有一个module a.py . 如何让Python知道这个文件层次结构?很简单,每个目录都放一个名为_init_.py 的文件.该文件内容可以为空.这个层次结构如下所示:
引用

parent
  --__init_.py
  --child
    -- __init_.py
    --a.py

b.py

那么Python如何找到我们定义的module?在标准包sys中,path属性记录了Python的包路径.你可以将之打印出来:
import sys
print(sys.path)

通常我们可以将module的包路径放到环境变量PYTHONPATH中,该环境变量会自动添加到sys.path属性.另一种方便的方法是编程中直接指定我们的module路径到sys.path 中:
import sys
import os
sys.path.append(os.getcwd()+'\\parent\\child')

print(sys.path)

from a import add_func


print (sys.path)

print ("Import add_func from module a")
print ("Result of 1 plus 2 is: ")
print (add_func(1,2))

引用

知识点:
1. 如何定义模块和包
2. 如何将模块路径添加到系统路径,以便python找到它们
3. 如何得到当前路径

分享到:
评论

相关推荐

    python3入门学习

    python3新手入门学习书籍。 如果你是小白用户,满足以下条件: 会使用电脑,但从来没写过程序; 还记得初中数学学的方程式和一点点代数知识; 想从编程小白变成专业的软件架构师; 每天能抽出半个小时学习。 不要再...

    python3 入门学习笔记

    python3 入门学习笔记

    Python3基础学习路线脑图

    Python3基础学习路线脑图

    零基础入门学习Python_零基础入门学习Python_小甲鱼_

    小甲鱼书籍,零基础入门学习Python,适合新手学习,B站有随书教学视频

    python 爬虫入门学习资料

    python 爬虫入门学习资料/python 爬虫入门学习资料/python 爬虫入门学习资料/python 爬虫入门学习资料 网盘资源

    Python3基础学习笔记.pdf

    Python 3.4基础学习笔记,系统全面的介绍Python的基本语法和高级特性,适合于Python初学者快速入门。

    Python编程基础入门教程 Python脚本入门学习经典手册 共67页.pdf

    Python编程基础入门教程 Python脚本入门学习经典手册 共67页.pdf

    【Python3】零基础入门学习Python--Python3.docx

    【Python3】零基础入门学习Python--Python3.docx

    Python基础入门学习

    最详细的Python基础入门学习,分解课时讲解,共计96课时,尽请大家学习

    人工智能实战,从 Python 入门到机器学习.zip

    人工智能实战,从 Python 入门到机器学习 人工智能实战,从 Python 入门到机器学习 人工智能实战,从 Python 入门到机器学习 人工智能实战,从 Python 入门到机器学习 人工智能实战,从 Python 入门到机器...

    Python3入门基础教程.pdf

    Python3⼊门基础教程 ⼊门基础教程 引:此⽂是⾃⼰学习python过程中的笔记和总结,适合有语⾔基础的⼈快速了解python3和没基础的作为学习的⼤纲,了解学习的⽅向、知 识点;笔记是从多本书和视频上学习后的整合版。 ...

    Python3入门指南_v2.4.pdf

    三、Python基础语法学习 30 3.1 编写第一个Python程序 30 3.2 Python中单行与多行注释语法 31 3.3 python输出功能基本语法:print() 32 3.4 python输入功能基本语法:input() 34 3.5 Python标识符与关键字 34 3.6 ...

    Python教程入门到精通:千锋Python语法基础学习视频教程.pdf

    基于此,千锋 Python 教学团队特别录制了 Python 教程入门到精通之千锋 Python 语法基础学习视频教程,只有对概念有深入的理解和剖析,才能打好基 础,实现从入门到精通质的飞跃。 千锋 Python 语法基础学习视频教 ...

    Python学习手册(第3版)中文版

    无论你是刚接触编程或者刚接触Python,通过学习《Python学习手册(第3版)》,你可以迅速高效地精通核心Python语言基础。读完《Python学习手册(第3版)》,你会对这门语言有足够的了解,从而可以在你所从事的任何应用...

    flare_zhao老师的python3入门人工智能

    flare_zhao老师的python3入门人工智能 掌握机器学习+深度学习,前4章代码及训练的数据集,有需要可以下载,对于入门是一个不错的选择

    Python零基础入门学习教程

    Python1和Python2两个教程均可,二选一即可,Python1包含安装包 ...【01】Python基础开发(零基础入门学习) 【5】python项目开发 【4】Pyhon实战开发 【3】python运维 【2】python进阶 【1】开发基础带安装包

    python基础入门(超详细).pdf

    python基础⼊门(超详细) 0x00 Python⼊门知识点 特来整理常见的top50⼊门知识点,初学者可以参考学习 1.input输出 password=(input("你的密码是:")) print("你的密码是:",password) 2.输出类型 a = 10 print...

    python基础入门课件

    python学习python学习python学习python学习python学习python学习python学习python学习

    python零基础入门学习

    python零基础入门学习课程,主要包括windows10系统下python环境变量配置,pycharm软件安装,python变量和关键字,python基础数据类型(包括字符串,布尔值,整型,浮点型),python进阶数据类型(列表,字典,元组,...

    Python学习手册第4版 中文PDF版 数10万Python爱好者的入门必读之作

    本书是学习Python编程语言的入门书籍。Python是一种很流行的开源编程语言,可以在各种领域中用于编写独立的程序和脚本。Python免费、可移植、功能强大,而且使用起来相当容易。来自软件产业各个角落的程序员都已经...

Global site tag (gtag.js) - Google Analytics