2.2变量
print("hello word")
message = "hello world"
print(message)
hello word
hello world
变量命名规则
- 变量名只能包含字母、数字和下划线。变量名可以字母或下划线打头,但不能以数字打头,例如,可将变量命名为message_1,但不能将其命名为1_message。(这条和java不太一样)
- 变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名greeting_message可行,但变量名greeting message会引发错误。
- 不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print(请参见附录A.4)。
- 变量名应既简短又具有描述性。例如,name比n好,student_name比s_n好,name_length比length_of_persons_name好。
- 慎用小写字母l和大写字母O,因为它们可能被人错看成数字1和0。
2.3字符串
用引号括起来的都是字符串,引号可以是单引号,也可以是双引号
也可以用三个引号来标记大段字符
a = 'this is "a"'
b = "this is 'b'"
c = """this is "c' """
print (a+","+b+","+c)
hello word
hello world
hello world for you
this is "a",this is 'b',this is "c'
2.3.1字符串的常用方法
title()
单词首字母大写,其他字母小写
upper()
lower()
# encoding:utf-8
name = "那些人heLLo WoRD"
# 单词首字母大写,其他字母小写
print(name.title())
print(name.upper())
print(name.lower())
# 那些人Hello Word
# 那些人HELLO WORD
# 那些人hello word
2.3.2合并(拼接)字符串
python使用加号(+)拼接字符串
2.3.3制表符或者换行符
\t
\n
print('Language\n\tPython\n\tC\n\tJava')
Language
Python
C
Java
2.3.4删除空白
rstrip():删除尾端的空白
lstrip():删除首端的空白
strip():删除首尾端的空白
word = '\t word \n'
print(word.title().strip() + "1")
print(word.lstrip() + "1")
print(word.rstrip() + "1")
Word1
word
1
word1
2.4 数字
2.4.1 整数
# encoding:utf-8
print(3+2)
print(3-2)
print(3*2)
print(3/2)
print()
print(3**2)
print(3+2*4)
print((3+2)*4)
5
1
6
1.5
9
11
20
2.4.2浮点数
# encoding:utf-8
print(3+2)
print(3-2)
print(3*2)
print(3/2)
print()
print(3**2)
print(3+2*4)
print((3+2)*4)
print('\n\b')
print(0.2+0.1)
print(0.2-0.1)
print(0.2*0.1)
print(0.2/0.1)
print(0.1+0.1)
print(0.2+0.2)
print(2*0.1)
print(2*0.2)
5
1
6
1.5
9
11
20
0.30000000000000004
0.1
0.020000000000000004
2.0
0.2
0.4
0.2
0.4
python2 vs python3 整数相除结果比较
2.4.3使用函数str()避免类型错误
# encoding:utf-8
age = 23
print("Happy"+str(age)+"rd BirthDay")
print("Happy"+age+"rd BirthDay")
# error message:
# Traceback (most recent call last):
# File "/Users/chujun/PycharmProjects/ahs/python_start_and_practice/first/second_chapter/str.py", line 3, in <module>
# print("Happy"+age+"rd BirthDay")
# TypeError: can only concatenate str (not "int") to str
2.5注释(#)
2.6 Python之禅
显示Tim Peters的python之禅
>>>import this
(venv) chujun@chujundeMacBook-Pro ~/PycharmProjects/ahs master python3
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 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!
>>>