【Python3.7学习笔记】三、变量和简单数据类型
【Python3.7学习笔记】一、环境搭建 【Python3.7学习笔记】二、第一个python程序 【Python3.7学习笔记】三、变量和简单数据类型 【Python3.7学习笔记】四、列表 【Python3.7学习笔记】五、字典
print("Hello My Python World!")
运行上面代码,输出:
Hello My Python World!
运行文件hello_world.py时,末尾的py指出这是一个Python程序,编辑器将使用python解释器运行它。python解释器读取整个程序,确定每个单词的含义。看到单词print时,解释器不管括号中的内容是什么,都会将括号中的内容打印到屏幕。
message = "Hello My Python World!"
print(message)
运行上面代码,输出:
Hello My Python World!
我们定义一个变量message,并给它赋值为Hello My Python World!,然后将它打印到屏幕。
Hello My Python World! Hello My Python perfect World!
修改变量的值,最新的值为变量的值
message = "Hello World!"
print(mesage1)
运行上述代码时,解释器会提示一个traceback信息,指出什么地方出错
NameError:name ‘message1’ is not defined 变量名没有定义
python中,定义:用单引号或双引号括起来的都是字符串,比如:
# -*- coding: utf-8 -*-
message = "我是一个程序猿"
print(message)
message = '我是一个python程序猿'
print(message)
message = '我是一个"python"程序猿'
print(message)
message = "我是一个'程序猿'"
print(message)
message = "我是一个python`s程序猿"
print(message)
运行结果:
python使用+号来拼接字符串
name = "ada lovelace"
print(name.title())
name = "Ada"
print("Ada=="+name)
print("Ada lower()=="+name.lower())
print("Ada upper()=="+name.upper())
print(name.lower().title())
name = "ADA"
print("ADA=="+name)
print("ADA lower()=="+name.lower())
print("ADA upper()=="+name.upper())
print(name.lower().title())
name = "ada"
print("ada=="+name)
print("ada lower()=="+name.lower())
print("ada upper()=="+name.upper())
print(name.lower().title())
# title()以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写
# lower()将单词的所有字母变成小写字母
# upper()将单词的所有字母变成大写字母
# 字符串使用+拼接
first_name = "ada"
last_name = "lovelace"
full_name = first_name +" "+last_name
print(full_name)
# 制表符\t或换行符\n添加空白
print("python")
print("\tPython")
print("Language:\nPython\nC\nJavaScript")
print("Language:\n\tPython\n\tC\n\tJavaScript")
# 删除空白
favorite_language = ' Python '
print(favorite_language)
# 两端删除空白
print(favorite_language.strip())
# 左侧删除空白
print(favorite_language.lstrip())
# 右侧删除空白
print(favorite_language.rstrip())
python中,可对整数执行加减乘除运算
>>> 1+1
2
>>> 4-2
2
>>> 2*4
8
>>> 3/2
1.5
>>> 2**2
4
>>> 2**3
8
>>> 2**4
16
>>> 2**5
32
>>> 2**6
64
>>>
>>> 2+3*4
14
>>> (2+3)*4
20
>>>
python将带小数点的数字称为浮点数,浮点数运算
>>> 0.1+0.1
0.2
>>> 0.2+0.2
0.4
>>> 2*0.1
0.2
>>> 2*0.3
0.6
>>>
浮点数小数位数不确定
>>> 0.1+0.2
0.30000000000000004
>>> 3*0.1
0.30000000000000004
>>>
# 使用函数str()避免类型错误
age = 23
#1 message = "Happy " + age + "rd Birthday!"
message = "Happy " + str(age) + "rd Birthday!"
print(message)
如果使用#1处的代码,python会提示错误:
python中,注释用#标识,python解释器会忽略#后面的内容
# 我是一个python程序猿
message = '我是一个python程序猿'
print(message)
编写注释的目的描述代码做什么,如何做。尽量编写有意义的注释
python编程遵循的灵魂
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
欢迎大家一起交流讨论