Python 11

一. 课上代码

>>> "{0} love {1}.{2}".format('I', 'FishC', 'com')
'I love FishC.com'
>>> "{a} love {b}.{c}".format('I', 'FishC', 'com')
Traceback (most recent call last):
  File "", line 1, in 
    "{a} love {b}.{c}".format('I', 'FishC', 'com')
KeyError: 'a'
>>> "{a} love {b}.{c}".format(a = 'I', b = 'FishC', c = 'com')
'I love FishC.com'
>>> print('\ta')
    a
>>> print('\\')
\

#此处二者对比一下
>>> "{{0}}".format("Noprinting")
'{0}'
>>> "{{1}}".format("Yes", "No")
'{1}'

#此处二者对比一下
>>> '{0:.1f}{1}'.format(27.658, 'GB')
'27.7GB'
>>> '{0}{1:.2f}'.format('Pi = ', 3.1415)
'Pi = 3.14'

>>> '%c' % 97
'a'
>>> '%c %c %c' % (97, 98, 99)
'a b c'
>>> '%s' % 'I love FishC.com'
'I love FishC.com'
>>> '%d + %d = %d' % (4, 5, 4+5)
'4 + 5 = 9'
>>> '%o' % 10
'12'
>>> '%x' % 10
'a'
>>> '%X' % 10
'A'
>>> '%f' % 27.658
'27.658000'
>>> '%e' % 27.658
'2.765800e+01'
>>> '%g' % 27.658
'27.658'
>>> '5.1f' % 27.658
Traceback (most recent call last):
  File "", line 1, in 
    '5.1f' % 27.658
TypeError: not all arguments converted during string formatting
>>> '%5.1f' % 27.658
' 27.7'
>>> '%.1f' % 27.658
'27.7'
>>> '%.2e' % 27.658
'2.77e+01'
>>> '%10d' % 5
'         5'
>>> '%-10d' % 5
'5         '
>>> '%+d' % 5
'+5'
>>> '%-d' % 5
'5'
>>> '%+d' % -5
'-5'
>>> '%#o' % 10
'0o12'

二. 测试题

  1. 还记得如何定义一个跨越多行的字符串吗?请至少写出两种实现的方法
>>> str1 = '''Dear Stella,
I love you,
I miss you,
I want you,
I need you.'''
>>> print(str1)
Dear Stella,
I love you,
I miss you,
I want you,
I need you.
>>> str2 = 'Dear Stella,\
I love you, \
I miss you, \
I want you, \
I need you. '
>>> print(str2)
Dear Stella,I love you, I miss you, I want you, I need you. 
>>> str2
'Dear Stella,I love you, I miss you, I want you, I need you. '
>>> str3 = ('Dear Stella,'
    'I love you,'
    'I miss you,'
    'I want you,'
    'I need you.')
>>> str3
'Dear Stella,I love you,I miss you,I want you,I need you.'
  1. file1 = open('C:\windows\temp\readme.txt', 'r')表示以只读方式打开"C:\windows\temp\readme.txt"这个文本文件,但事实上这个语句会报错,知道为什么吗?应该如何修改
#会报错因为"\t"和"\r"分别表示"横向制表符"和"回车符"
>>> file1 = open(r'C:\windows\temp\readme.txt', 'r')

三. 动动手

  1. 编写一个进制转换程序,程序演示如下(提示,十进制转换二进制可以用bin()这个BIF)


    Python 11_第1张图片
#个人代码
import sys
number = input("Please input a number(closing the program using Q): ")
if number == 'Q':
    sys.exit()
else:
    num = int(number)
    print('shijinzhi -> shiliujinzhi : {} -> {}'.format(num, '%#x' % num))
    print('shijinzhi -> bajinzhi : {} -> {}'.format(num, '%#o' % num))
    print('shijinzhi -> erjinzhi : {} -> {}'.format(num, bin(num)))
#参考代码
q = True
while q:
    num = input("Please input a number(closing the program using Q): ")
    if num != 'Q':
        num = int(num)
        print('shijinzhi -> shiliujinzhi : {} -> {}'.format(num, '%#x' % num))
        print('shijinzhi -> bajinzhi : {} -> {}'.format(num, '%#o' % num))
        print('shijinzhi -> erjinzhi : {} -> {}'.format(num, bin(num)))
    else:
        q = False
#应该注意到图片中如果不输入Q程序不停止,那么就应该先True一下程序才行
  1. 编写一个密码安全性检查的代码,要求如下:
    (1) 低级密码要求:
    a. 密码由单纯的数字或字母组成
    b. 密码长度小于等于8位
    (2)中级密码要求:
    a. 密码必须由数字、字母或特殊字符(仅限:!@#$%^&*()_+-=|{}[]:;"<>',.?/)任意两种组合
    b. 密码长度不能低于8位
    (3)高级密码要求:
    a. 密码必须由数字、字母或特殊字符三种组合
    b. 密码只能由字母开头
    c. 密码长度不能低于16位


    Python 11_第2张图片

你可能感兴趣的:(Python 11)