基础知识

您所在的位置:网站首页 数据类型命名 基础知识

基础知识

2023-04-15 12:57| 来源: 网络整理| 查看: 265

变量

#输入一段内容

print('hello world')

# 将hello world添加到变量message中,再使用print打印

message = "hello world"

print(message)

# 此时我们修改变量内容

message = "你好,世界"

print(message)

#可以看到最后输出的内容是 最后更新的变量内容,由此可以知道,变量始终记录的是最新的值

变量的命名和使用

python的命名需要准守规则,如果违反规则会发生错误,请务必遵守规则。

•变量名只能使用字母、数字、下划线。但不能使用数字开头

•变量名不能包含空格。

•不要将python关键字和函数名用作变量名。

•变量名应该简短又具有描述性

•慎用l和o,容易看错为1和0

注意:变量名应使用小写字母,文件名使用小写字母和下划线。

练习:

# 练习一 使用变量将一条消息存储到变量中,再打印出来

message = '欢迎'

print(message)

# 练习二  多条简单消息,将一条消息存储到变量中,将其打印出来,再将变量的值改为新消息,在将其打印

message1 = '你好'

print(message1)

message1 = '世界'

print(message1)

字符串(string)

字符串就是一系列字符,在Python中。用引号括起来,可以是单引号也可以是双引号

如:

'this is string'

"this is string"

使用方法修改字符串的大小写

对于字符串,最简单的操作就是转换大小写

如:

name = "love & love"

print(name)

#使用title()方法将首字母大写

#在python中,方法是对数据可以执行的操作,使用. 后面跟方法,方法后使用()

print(name.title())

如:

name = "love & love"

print(name)

print(name.upper())#字母大写

print(name.lower())#字母小写

合并(拼接)字符串

多数情况下,我们需要拼接字符串,使用 + 号进行拼接。

如:

first_name = 'georgr'

last_name = 'zhang'

print(first_name + " " + last_name)

将拼接后的完整名字保存到变量中

first_name = 'george'

last_name = 'zhang'

full_name = first_name + " " + last_name + '!'

print('hello '+ full_name.title())

使用制表符或者换行符来添加空白

编程中,空白泛指任何非打印字符,如空格、制表符、换行符。你可以使用空白来组织输出,使其更加易读。

如:

#添加空格print('\tpython')

#添加换行符print('python\njava')

#组合使用

print('languages:\n\tpython\tjava\tc')

删除空白

python可以找出字符的开头和结尾的空白。可以使用rstrip()

例:

message4 = 'python '

print(message4)

print(message4.rstrip())

print(message4)

注意:可以看到再次打印message4变量,空白依然存在,所以想要永久删除空白需要将结果重新赋值给message4。

例:

message5 = 'python '

message5 = message5.rstrip()

print('message5',message5)

还可以删除字符串开头空白,或者同时删除字符串两端的空白。使用的方法为lstrip()和strip()。

例:

message6 = ' python '

print(message6.lstrip())#删除开头空白

print(message6.strip())#同时删除两端空白

使用字符串时避免语法错误

语法错误是一种时不时就会遇到的问题,程序中包含非法的python语句时,就会导致语法错误。

注意:当编写程序时,编辑器会突出语法错误,可以根据提示快速定位问题。

数字整数

在python中,可以对整数执行+ - * / 运算

浮点数

python将带有小数点的数字称为浮点数,大多数编程语言都使用了这个术语。

使用函数str()避免错误类型

例如,我们要祝某个人生日快乐,可以编写这样的代码

age = 23

bir = 'happy' + str(age) + 'birthday'

print(bir)

#此时会报 TypeError: can only concatenate str (not "int") to str的错误。因为字符串无法于数字进行拼接。需要使用str()函数进行转换。

age = 23

bir = 'happy ' + str(age) + ' birthday'

print(bir)

#将int类型转换为str类型,再进行拼接,此时python程序正常运行

注释

在python中,使用#号进行注释。#号之后的内容会被python解释器忽略。

如何编写注释

编写注释的主要目的是阐述代码要做什么,以及如何去做,在开发过程中,你对于各个部门如何协同工作了如指掌,但是过了一段时间有些细节已经忘记了,这时你可以通过注释来了解。

作为新手,注释是最值得养成的习惯之一,在代码中编写清晰、简洁的注释

最后读一下Python之禅:开始深入学习。

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!

顶顶顶



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3