CH2 变量和简单数据类型

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 变量的命名和使用

  1. 字母、数字、下划线组成,不能以数字打头 message_11_message
  2. no space, use underscores
  3. 不要使用python关键字或函数名
  4. 短且具有描述性

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值