简明python教程实例

《简明python教程》是名副其实最合适的入门教程,跟着英文原版的改进,中文译本同时也有了新的版本,适合python3,python3和python2语法上有一些差距,这里总结一些python3的应用实例,一是总结学习,二是方便查找基础知识。

1.print打印

print('Hello World!')
print("Hello World!")
2.'''或'''指定多行字符串

m_str='''这是一段多行字符串,这是第一行
this is the second line.
"wha's your name?," Iasked.
He said "Bond, James Bond".
'''
print(m_str)
3.格式化输出

name = 'python'
age = 20
print('name = {0}, age = {1}'.format(name,age))
print("name = %s, age = %d" %(name,age))
print(name + ' is ' + str(age) + ' years old')
4.用end指定print的打印结尾,默认是\n
print('a', end="")
print("b", end="")

print('c', end = ' ')
print('d', end = ' ')
5. \ 显式行连接

s = 'This is a string \
this continues the string'

print(s)
6.表达式(print打印的逗号,必须要,否则提示语法错误,同时会自动添加空格调整显示结果,不需要添加最后的空格)
length = 5
breadth = 2

area = length*breadth
print('Area is', area)
print('Perimeter is', 2*(length + breadth))
7.if语句
number = 23
guess = int(input('Enter an integer : '))

if guess == number:
    #新块从这里开始
    print('Congratulations, you guessed it.')
elif guess < number:
    print('No, it is a little higher than that.')
else:
    print('No, it is a little lower than that.')

print('Done')
8.while语句

number = 23
running = True

while running:
    guess = int(input('Enter an integer : '))

    if guess == number:
        #新块从这里开始
        print('Congratulations, you guessed it.')
        #这将导致while循环中止
        running = False
    elif guess < number:
        print('No, it is a little higher than that.')
    else:
        print('No, it is a little lower than that.')
else:
    print('The while loop is over.')

print('Done')
9.for语句
for i in range(1, 5):
    print(i)
else:
    print('the for loop is over')
10.break语句

while True:
    s = input('Enter something : ')
    if s == 'quit':
        break
    print('Length of the string is', len(s))
print("Done")
11.continue语句
while True:
    s = input('Enter something : ')
    if s == 'quit':
        break
    if len(s) < 3:
        print('too small')
        continue
    print('Length of the string is', len(s))
print("Done")

12.函数一(简单定义)

def say_hello():
    #函数块
    print('Hello World')

say_hello()
13.函数二(带参数的函数)
def print_max(a, b):
    if a > b:
        print(a, 'is maximum')
    elif a == b:
        print(a, 'is equal to', b)
    else:
        print(b, 'is maximumu')

print_max(3, 4)
14.局部变量
x = 50

def func(x):
    print('x is', x)
    x = 2
    print('changed local x to', x)

func(x)
print('x is still', x)
15.global全局变量(不推荐这么做)

x = 50

def func():
    global x
    print('x is', x)
    x = 2
    print('changed local x to', x)

func()
print('value of x is', x)
16.函数三(带默认参数的函数)

def say(message, times = 1):
    print(message * times)

say('Hello')
say('World', 3)
17.函数四(关键字参数)

def func(a, b=5, c=10):
    print('a is', a, 'and b is', b, 'and c is', c)

func(3, 7)
func(25, c = 24)
func(c= 50, a =100)
18.可变参数

def total(a=5, *numbers, **phonebook):
    print('a', a)

    #遍历元组中左右项目
    for single_item in numbers:
        print('single_item', single_item)

    #遍历字典中的所有项目
    for first_part, second_part in phonebook.items():
        print(first_part, second_part)

print(total(10,1,2,3,Jack=1123,John=2231,Inge=1234))
当我们声明一个诸如*param的星号参数时,从此处开始直到结束的所有位置参数都将被收集并汇集成一个称为"param"的元组。

类似地,当我们声明一个诸如**param的双星号参数时,从此处开始直到结束的所有关键字参数都将被收集汇集到一个名为param的字典。
19.return语句

def maximum(x, y):
    if x > y:
        return x
    elif x == y:
        return 'The numbers are equal'
    else:
        return y

print(maximum(2, 3))

内容太多时,不方便查看,后续内容接第二节



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值