美文网首页
第二章 变量和简单数据类型

第二章 变量和简单数据类型

作者: 仙境源地 | 来源:发表于2019-11-26 16:11 被阅读0次

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 整数相除结果比较


image.png

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!
>>> 

相关文章

  • Python 复习

    第二章 变量和简单数据类型 第三章 列表 list

  • 《Python编程:从入门到实践》各章小结

    第二章 变量和简单数据类型 对于给定变量赋值,在程序中可以随时改变该变量值,而python将始终记录变量的最新值。...

  • 第一课:运行hello_world.py和变量

    预习任务: .py、 笔记: 第二章 变量和简单数据类型 2.1运行hello_world.py发生的情况 知识点...

  • Python编程:从入门到实践 Day2

    第二章:变量和简单数据类型 1.变量的命名和使用 (1)命名规则: 1.只能包含字母、数字和下划线,且不可以用数字...

  • Python学习(二)

    第二章 基础语法 2.1 数据类型 2.1.1 Python变量的赋值和基本数据类型 Python中的变量不需要声...

  • Python基础

    第二章 变量和简单数据类型 运行py文件时发生的情况运行hello_world.py文件时,末尾的.py指出这是一...

  • Python入门学习笔记-1

    第二章 变量和简单数据类型 字符串 Python中引号扩起来的都是字符串(双引号或单引号) "Hello Worl...

  • 简单数据类型和复杂数据类型

    简单数据类型和复杂数据类型 1.1 简单数据类型 ​ 简单类型(基本数据类型、值类型):在存储时变量中存储的是值本...

  • Python 个人笔记(1)

    《第一章》Python的安装 《第二章》变量和简单数据类型 字符串 命名和使用:首字母不能为数字 修改字符串的大小...

  • python变量和简单的数据类型

    python第一课:变量和简单的数据类型

网友评论

      本文标题:第二章 变量和简单数据类型

      本文链接:https://www.haomeiwen.com/subject/tjsewctx.html