笨办法学PYTHON3 习题01 第一个程序
上手根据习题直接撸代码也未尝不可,事后回过头来总结,我觉得写代码首先要看一下代码书写规范。
毕竟优雅永不过时
好的让我们正式开始
- 习题01:将上面的内容写到一个文件中,取名为 ex1.py。
这个命名方式很重要,Python 文件最好以 .py 结尾。
# -*- coding: utf-8 -*-
"""
@lenrn Python the hard way ex1 :the first program
@author:Aoli
@create:2022-2-21
"""
print("Hello World!")
print("Hello again")
print("I like typing this.")
print("This is fun.")
print("Yah!Printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this')
学习心得:注意代码最后两段中,引号与双引号的区别,当字数串中引号会出现歧义是可用单双引号进行区分
- 运行效果:
Hello World!
Hello again
I like typing this.
This is fun.
Yah!Printing.
I’d much rather you ‘not’.
I “said” do not touch this
- 附加题1.1 让你的脚本多打印一行
# 只需要在以上脚本末行添加一行即可
print("\n")
- 附加题1.2 让你的脚本只打印一行
print("Hello World!",end = " ")
print("Hello again",end = " ")
print("I like typing this.",end = " ")
print("This is fun.",end = " ")
print("Yah!Printing.",end = " ")
print("I'd much rather you 'not'.",end = " ")
print('I "said" do not touch this',end = " ")
- 运行效果:
Hello World! Hello again I like typing this. This is fun. Yah!Printing. I’d much rather you ‘not’. I “said” do not touch this
知识点 此处用到python内置参数print的内建方法将多段话直接打印成一行,更多相应的方法后面会慢慢学
,此处我们使用 end = " "将各段话隔开并拼接成一行字符串
- 附加题1.3 在一行的起始位置放一个 ‘#’
(octothorpe) 符号。它的作用是添加注释
注释可以用来解释代码 提高代码的可读性.
在测试代码时添加注释,可用用来组织执行.
井号有很多的英文名字,例如:’octothorpe(八角帽)’,’pound(英镑符)’,
‘hash(电话的 # 键)’, ‘mesh(网)’ 等。