Tasks
- 数据类型
- 数据储存到变量
- 如何在程序中使用变量
2.1
# Chapter 2 Variable
print("hello world!")
#2.2 vatiable
message = "hello world!"
print(message)
message = "hello world! this line is writtend in 2021/1/3 18:11"
print(message)
#variable is changeable, and python always recoded the newest value
2.2 变量的命名和使用
- 字母、数字、下划线组成,不能以数字打头
message_1
,1_message
- no space, use underscores
- 不要使用python关键字或函数名
- 短且具有描述性
error and traceback
2.3 string 字符串
大小写
- 首字母大写
#2.3 string
# uppercase and lowercase
name = "song yaxuan"
print(name.title())
where title()
is a method, 方法. 方法是python对数据执行的操作。
name.title()
:对变量name
执行方法title()
制定的操作
- 全大写/全小写
print(name.upper())
print(name.lower()) #often use for recording data
合并(拼接)
+
# amend(?)
surname = "yaxuan"
family = "song"
full_name = family + " " + surname
print(full_name)
print("Hello, " + full_name.title() + "!")
message = ("Hello, " + full_name.title() + "!")
print(message) #neat code
制表符和换行符
\t
制表符
\n
换行符
# \t, \n
print("Languages:\n\tPython\n\tC\n\tJavaScript")
print("tfent:\n\tTFBoys\n\t\tKarry Wang\n\t\tRoy Wang\n\t\tJackson Yi\n\tTeens in Times\n\t\ttoo many members\n\t\t...click to see complete member names")
删除空白
删除用户输入数据中多余的空白们,剥除函数
rstrip()
删除末尾空白
lstrip()
删除开头空白
strip()
同时剔除两段空白
NB通常用于在储存用户输入前对其进行清理
语法错误
引号的使用
# grammer error
message = "Johnny's communication center"
print(message)
'Johnny's communication center'
print()
print("Hi, what's your name?")
name = input()
print("hello " + name.title() + ", here is you daily playlist: \n\tNCT 127 - welcome to my playground")
print("the lower case of your name is " + name.lower())
print("the upper case of your name is " + name.upper())
name = " NEO CULTURE TECHNOLOGY "
print("the name with space is \n\t'" + name + "'\nthe name with no space at the beginning is \n\t'" + name.lstrip() + "'\nthe name with no space at the end is \n\t'" + name.rstrip() + "'\nthe name with no space is \n\t'" + name.strip() + "'")
2.4 numbers
作用
- 记录得分
- 可视化数据
- 储存web应用数据
整数
普通四则运算:+
-
*
/
乘方**
print(3/2)
result = 3/2
print(result)
浮点数
带有小数点的数字
使用str()
把非字符串转化为字符串
2.5 注释
use #
, the following information will be ignored by python
print("the name of your ship here:")
cp = input()
print("the fit index of " + cp + " is 100")
#zen of Python
import this
sum up
method:
title()
, upper()
, lower()
function:
print()
, input()
\n\t