博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

01python语言程序设计基础——初识python

Posted on 2019-04-29 20:31  心默默言  阅读(445)  评论(0编辑  收藏  举报
 

1.python的字符串中format函数用法

format 函数可以接受不限个参数,位置可以不按顺序。

In [1]:
"{} {}".format("hello", "world")  # 不设置指定位置,按默认顺序
Out[1]:
'hello world'
In [2]:
"{0} {1}".format("hello", "world")  # 设置指定位置
Out[2]:
'hello world'
In [3]:
"{1} {0} {1}".format("hello", "world")  # 设置指定位置
Out[3]:
'world hello world'
 

也可以设置参数:

In [4]:
print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
 
网站名:菜鸟教程, 地址 www.runoob.com
In [5]:
# 通过字典设置参数
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site))
 
网站名:菜鸟教程, 地址 www.runoob.com
In [6]:
# 通过列表索引设置参数
my_list = ['菜鸟教程', 'www.runoob.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # "0" 是必须的
 
网站名:菜鸟教程, 地址 www.runoob.com
In [7]:
class AssignValue(object):
    def __init__(self, value):
        self.value = value


my_value = AssignValue(6)
print('value 为: {0.value}'.format(my_value))  # "0" 是可选的
 
value 为: 6
 

2.数字格式化下表展示了 format() 格式化数字的多种方法:

In [8]:
print("{:.2f}".format(3.1415926))  # 保留小数点后两位
 
3.14
 

3.输出当前计算机系统的日期和时间

In [9]:
from datetime import datetime  # 引用datetime 库

now = datetime.now()  # 获得当前日期和时间信息
print(now)
 
2021-10-17 19:25:31.336885
In [10]:
now.strftime("%x")  # 输出其中的日期部分
Out[10]:
'10/17/21'
In [11]:
now.strftime("%X")  # 输出其中的时间部分
Out[11]:
'19:25:31'
In [12]:
now.strftime("%Y-%m-%d")
Out[12]:
'2021-10-17'
In [13]:
import time

print('{}BiasedMF312and4414_rt.txt'.format(time.strftime("%Y-%m-%d")))
 
 
2021-10-17BiasedMF312and4414_rt.txt
 

4.九九乘法表

In [14]:
for i in range(1, 10):
    for j in range(1, i + 1):
        print("{}*{}={:2}".format(j, i, j * i), end='  ')
    print('')
 
1*1= 1  
1*2= 2  2*2= 4  
1*3= 3  2*3= 6  3*3= 9  
1*4= 4  2*4= 8  3*4=12  4*4=16  
1*5= 5  2*5=10  3*5=15  4*5=20  5*5=25  
1*6= 6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
1*7= 7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
1*8= 8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
1*9= 9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  
 

5.猴子吃桃问题:

 

猴子第一天摘下若干个桃子,吃了一半多一个,以后每天都吃剩下的一半多一个,第五天只剩下一个桃子,问猴子第一天共摘了多少个桃子?

In [15]:
n = 1
for i in range(5, 0, -1):
    n = (n + 1) << 1  #  相当于乘以2
print(n)
 
94
 

6.利用格式化输出和时间延迟实现文本进度条

In [17]:
import time

scale = 10
print("{0:-^30}".format("执行开始"))
for i in range(scale + 1):
    a, b = '**' * i, '**' * (scale - i)
    c = (i / scale) * 100
    print("%{:^3.0f}[{}->{}]".format(c, a, b))
    time.sleep(0.1)
print("{0:-^30}".format("执行结束"))
 
-------------执行开始-------------
% 0 [->********************]
%10 [**->******************]
%20 [****->****************]
%30 [******->**************]
%40 [********->************]
%50 [**********->**********]
%60 [************->********]
%70 [**************->******]
%80 [****************->****]
%90 [******************->**]
%100[********************->]
-------------执行结束-------------
 

7.lamda函数——<函数名> = lamda <参数列表>:<表达式>

In [18]:
f = lambda x, y: x + y
f(10, 12)
Out[18]:
22
In [19]:
f2 = lambda a, b, c: a * b + c
f2(1, 2, 3)
Out[19]:
5
 

8.递归:字符串反转

In [20]:
def reverse(s):
    if s == "":
        return s
    else:
        return reverse(s[1:]) + s[0]
In [21]:
str = "你好我好大家好"
reverse(str)
Out[21]:
'好家大好我好你'