Python 基本语句 (5) 持续更新

前面几张大家都已经看到了 print语句、import语句、赋值语句。这次主要是深入的了解更多语句。

语句(1) print

用逗号输出

print ------ 无论是字符串还是其他类型都会自动转成字符串并输出,如果需要打印多个,只要用逗号隔开就可以了。

>>> print ‘My name is ‘, ‘Zhao‘
My name is  Zhao

print 参数并不会构成元组,如果要变成元组,就需要用括号,括起来。

>>> print 1, 2, 3
1 2 3
>>> print (1,2,3)
(1, 2, 3)

如果 print 语句的末尾有逗号,那么接下来的 print 语句 会在 同一行打印。

print ‘Hello‘,
print ‘Python‘

输出

Hello Python

语句(2) import

当想调用模块(module)函数(function)的时候,首先要先导入(import)一下。 导入的方式有

  1. import module
  2. from module import function
  3. from module import function,anotherfunction,yetfunction
  4. from module import *

如果两个模块当中,都有一个同名的函数 如 set 函数,那只要使用第一种方式导入,就可以了。如

import module
import anothermodule
module.set()
anothermodule.set()

还可以给模块起别名。如

import module as fish
fish.eat()

为函数起别名。如

from module import function as fishfun
fishfun()

对于那些同名的函数,那么就可以这样使用 如

from module1 import function as function1
from module2 import function as function2

注意 有一些模块是分层次安排的,(一个模块在另一个模块的内部)详细的内容,会在后面更新。

语句(3) 赋值

多个变量赋值 称为 序列解包 或 递归解包

>>> a, b, c = 1, 2, 3
>>> a,b,c
(1, 2, 3)

交换变量(或多个变量)

>>> a, b = 1, 2
>>> print a, b
1 2
>>> a, b = b, a
>>> print a, b
2 1

当函数或者方法返回元组、序列或可迭代对象时,都可以利用这个特性。

>>> words = {‘one‘:1,‘two‘:2,‘three‘:3}
>>> k,v = words.popitem()
>>> k,v
(‘three‘, 3)

注意 序列解包赋值时,左边的赋值变量必须与右边的元素数量相同,否则会引发赋值异常。

>>> a, b, c = 1,2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

Python3.0 当中更新的赋值解包新特性:用 * 运算符,把多余的参数,以列表的形式保存变量中。

>>> a, b, *c = 1,2,3,4,5,6,7,8,9
>>> a,b,c
(1, 2, [3, 4, 5, 6, 7, 8, 9])

一个变量赋值给多变量 ------- 链式赋值

>>> a, b = 1,2
>>> a, b
(1, 2)

表达式运算符( +、-、/、*、| )放在赋值运算符左边(=)   --------------  增量赋值

首先下面的两个语句是相等的

a = a + 1
a += 1

其中第二个语句就是 增量赋值,对于其他的类型也一样可以用。

str = ‘hello‘
str += ‘ word! ‘
print str
str *= 2
print str

输出

hello word!
hello word! hello word!

语句(4) 语句块

语句块是在 条件语句 和 循环语句 一组语句,它们需要用空格符或tab字符来缩进语句创建语句块。

用 : 表示当前语句块的开始,当回退语句块或闭合语句块缩进时,表示当前语句块结束。 如

a = 1
if a == 1 :
    print ‘A is equal to 1‘
else
    print ‘A is not equal to 1‘

语句(5) 条件语句

布尔值

下面的值都会Python看作 假 (false) :

False None  0 "" ()  [] {}

其他的都解释为真(true)。

时间: 2024-08-08 05:24:35

Python 基本语句 (5) 持续更新的相关文章

从代码中学Python语法二(持续更新)

# -*- coding:utf-8 -*- __author__ = 'hunterhug' #x = int(input("请输入一个数:\n")) x=5 if x < 0: print("<0") elif x == 0: print("0") else: print(">0") str=['fds', 'sdfsdf', '2',"h"] for k in str: print(

git的常用语句(持续更新中)

mkdir learngit // 创建文件夹cd learngit //跳转到指定文件夹pwd //显示当前文件夹路径git init //把这个目录变成可管理的仓库git add learngit.txt // 添加文件到提交目录git commit -m"文件相关信息" // 提交文件并附属文件所更改的描述信息git status //常看当前状态,用于描述文件所更新过的相关状态git diff //用于查看所更改过的相关信息git log //用于查看更改过的日志文件git r

从代码中学Python语法六(持续更新)

# -*- coding:utf-8 -*- class MyClass: """A simple example class""" i = 12345 def __init__(self, l, r): self.data = [] self.l=l self.r=r def f(self): return 'hello world' print(MyClass.i) print(MyClass.__doc__) x=MyClass(1,2)

Python文档参考(持续更新2017/08/09)

Python入门指南(基于3.6.0版本),在线阅读学习链接:http://www.pythondoc.com/pythontutorial3/index.html 简单python教程(基于3.5.1版本),在线阅读学习链接:https://bop.molun.net

从代码中学Python语法三(持续更新)

# -*- coding:utf-8 -*- from collections import deque __author__ = 'hunterhug' mylist = [2, 3, 4, 5] print(mylist) mylist.append(3) # 添加元素到链尾 print(mylist) mylist[mylist.__len__():]=[5] print(mylist) mylist.extend(mylist) # 链表叠加 print(mylist) mylist.i

python 100例 (持续更新)

1.题目:列表转换为字典. 程序源代码: 1 #!/usr/bin/env python 2 # -*- coding: UTF-8 -*- 3 4 i = ['a', 'b'] 5 l = [1, 2] 6 print dict([i, l]) 以上实例输出结果为: {'a': 'b', 1: 2} 2.题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中. 程序源代码:

Python 安全类目推荐 (持续更新)

推荐学习书目 › Learn Python the Hard Way › Python 学习手册 › Python Cookbook › Python 基础教程 Python Sites › PyPI - Python Package Index › http://www.simple-is-better.com/ › http://diveintopython.org/toc/index.html › Pocoo › Awesome Python 安全开发最常用的开发库,建议每个人都能熟练使用

【从0开始Tornado建站】0.9版本python网站代码开源--持续更新中

从5月份开始[从0开始Tornado建站]这个专栏,开始一点一点把这个分类兴趣网站弄起来,从无到有的过程也是令人兴奋的:-) 国庆的时候等待备案然后上线,现在网站域名为ustchacker.com, 欢迎加入,一起分享代码和点子,可以提问题共同进步- 现在的代码版本是0.9(我自己定的...因为还在完善中),希望能依靠开源的力量打造出一个分类兴趣网站的模板式框架!托管在GitHub上: https://github.com/littlethunder/ustchacker.com ,希望大家对p

Mysql(Mariadb) 基础操作语句 (持续更新)

基础SQL语句,记录以备查阅.(在HeiDiSql中执行) # 创建数据库 Create Database If Not Exists VerifyIdear Character Set UTF8; # 创建表 Create Table If Not Exists VerifyIdear.MyTable( ID Bigint(8) unsigned Primary key Auto_Increment, Updatetime DateTime, name VarChar(128) )Engine