python代码调试(一)——字面常量、变量

本文深入探讨了Python编程中常见的语法错误,包括字面常量的正确书写方式、变量定义及使用规范,以及如何避免类型错误和格式化字符串时的常见陷阱。通过具体的错误示例和修正方法,帮助读者理解并掌握Python编程的基本原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录:

一:字面常量

二:变量

 

一:字面常量

1 print ('i l'ove've fishc.com')

SyntaxError: invalid syntax

#print ('i l"ove" fishc.com')   or     print ("i l'ove' fishc.com")

 

2TypeError: cannot concatenate 'str' and 'int' objects

只有提前把num转换为字符串类型,可以使用bytes函数把int型转换为string型。

print ('123:' + bytes(name))

 

3Format 使用

 print('{name} wrote {book}'.format(name= 'www'),book = 'jjjj')           KeyError: 'book'

print('{name} wrote {book}'.format(name= 'www',book = 'jjjj'))

 

4print("This is the first sentence.

This is the second  sentence

SyntaxError: EOL while scanning string literal

print("This is the first sentence.\

this is the second sentence")

 

二:变量

1s = '''this is a multi-line string.

this is the second line.'''

print(s)

 

s = '''this is a multi-line string.

this is the second line.'''

print(s)

输入这种 居然不算重复

 

2所谓物理行(Physical Line)是你在编写程序时 你所看到 的内容。

  所谓逻辑行(Logical Line)是 Python 所看到 的单个语句

 

3  放置在一起的语句必须拥有相同的缩进。每一组这样的语句被称为 块(block)

 

4   print(i)

    ^

IndentationError: unexpected indent

→ 注意缩进

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值