图零直播

Python3的入门及语法精简版

发布用户:tolink789 +收藏

发布时间:2018-03-28 11:09:24 已有69人浏览

 


今天图零小编就带大家学习Python3的入门及语法,喜欢的同学快点收藏。

Python3.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0在设计的时候没有考虑向下兼容。

如何查看python版本:

我们可以使用以下命令来查看我们使用的Python版本:

python -V

以上命令执行结果如下:

Python 3.3.2

你也可以进入Python的交互式编程模式,查看版本:

Python3.x程序

对于大多数程序语言,入门编程代码便是"Hello World",以下代码为使用Python输出"Hello World"

实例(Python 3.0+)

#!/usr/bin/python3 print("Hello, World!");
运行实例 »

你可以将以上代码保存在hello.py文件中并使用python命令执行该脚本文件。

$ python3 hello.py

以上命令输出结果为:

Hello, World!

Python 是种易学而强大的编程语言. 它包含了高效的高级数据结构, 能够用简单而高效的方式进行面向对象编程.Python 优雅的语法和和动态类型,以及它天然的解释能力, 使其成为了大多数平台上能广泛适用于各领域的理想脚本语言和开发环境.

Python 的解释器可以很容易的通过 C 或者 C++ (或者其它可以通过 C 调用的语言) 扩展新的函式和数据类型. Python 也可以作为定制应用的扩展语言.

语法:

1 你好

#打开新窗口,输入:
#! /usr/bin/python
# -*- coding: utf8 -*- 
s1=input("Input your name:")
print("
你好,%s" % s1)
'''
知识点:

  * input("某字符串")函数:显示"某字符串",并等待用户输入.
    * print()
函数:如何打印.
    * 
如何应用中文
    
如何用多行注释
'''    

2 字符串和数字

但有趣的是,javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异,如下:

#! /usr/bin/python
a=2
b="test"
c=a+b
运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换

#! /usr/bin/python

#运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换
a=2
b="test"
c=str(a)+b
d="1111"
e=a+int(d)
#How to print multiply values
print ("c is %s,e is %i" % (c,e))
'''
知识点:
    * 
intstr函数将字符串和数字进行转换
    * 
打印以#开头,而不是习惯的//
    * 
打印多个参数的方式
    
'''

3 列表

#! /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)

'''
知识点:

    * 
列表长度是动态的,可任意添加删除元素.
    * 
用索引可以很方便访问元素,甚至返回一个子列表
    * 
更多方法请参考Python的文档
'''

图零科技欢迎你和我互动哦!扫描下方二维码关注图零直播微信公众号获取更多资讯!

关注图零直播微信公众号,点击菜单栏“微我吧”,“Q我吧”即可加我好友,与我更多互动!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


上一篇:上海图零科技有限公司——安徽科技学院领导到访图零学院

下一篇:全栈工程师脱颖而出的优势分析

QQ咨询

微信咨询