Python Variables, Constants and Literals

Python变量、常量、文本

Python Variables

内存用于在内存中存储变量(A variable is a named location used to store data in the memory)。可以把变量当做一个容器,我们可以在程序中更改其值。

number = 10;

在这里,我们创建了一个变量number,我们给这边变量分配了数字10。

可以把变量当做一个书包,里面的书可以随时更换。

number = 10;
number = 1.01;

最初,变量number的值是10,稍后其值变为了1.01。

Note: In Python, we don't assign values to the variables, whereas Python gives the reference of the object (value) to the variable.

(注意:在Python中我们不给变量赋值,反而是Python将对象(值)的引用给变量)

Assigning a value to a Variable in Python

我们可以使用操作符 "=" 给一个变量赋值。

Example 1: Declaring and assigning a value to a variable

name = 'Tom';

print(name) ; #结果为Tom

在代码中,我们给变量name分配了一个值 "Tom" 。然后我们打印分配给变量name的值,即Tom。

Note : Python is a type inferred language; it can automatically know apple.com is a string and declare website as a string.

(Python是一种自动类型转换语言,他能自己知道Tom是一个字符串,所以他将变量name声明为字符串)

Example 2: Changing the value of a variable

name = "Tom" ;
print(name);

#assiging a new variable to name
name="Jerry";
print(name);

代码中,我们一开始给变量name赋值Tom,然后将值变为了Jerry。

Example 3: Assigning multiple values to multiple variables

a,b,c = 99, 0.55, "Tom" ;

print(a);  #结果为99
print(b);  #结果为0.55
print(c);  #结果为Tom

代码中,一次给三个不同的变量赋值。

如果想一次性给多个变量赋相同的值,可以这么干

x=y=z=100;

print(x); #值为100
print(y); #值为100
print(z); #值为100

Constants

常量

常量是一种变量类型,常量的值无法改变。

Assigning value to a constant in Python

在Python中,经常在模块中声明并分配常量。这里的模块意味着一个新文件,包含着变量、函数等,可以被 import 进其它文件。在模块里,常量用大写字母和下划线命名。

#constant.py
PI = 3.14;

GRAVITY = 9.8 ;
#main.py

import constant

print(Constant.PI);
print(Constant.GRAVITY);

main.py的执行结果是:3.14  9.8

Note: In reality, we don't use constants in Python. The globals or constants module is used throughout the Python programs.

我们创建了一个constant.py模块文件。然后我们给常量PI 和 GRAVITY 分配值。

之后,我们创建一个main.py,并且import constant 模块。最后,我们在main.py中打印常量的值。

注意:实际上我们不在Python中使用常量。global 或者constant 模块用于Python编程中。

Rules and Naming Convention for Variables and constants

  1. 大小写英文字母、数组、下划线,结合命名
  2. 命名有意义,人搭眼一看能看懂
  3. 变量名有两个单词,用下划线隔开
  4. 常量的生命必须用大写字母
  5. 不要用特殊字符: !, @, #, $, % 等
  6. 变量开头不能是数字

Literals

文本是在变量或常量中给定的原始数据。

在Python中,有各种类型的文本,它们如下所示:

Numeric Literals

数字文本是不可变的(不可更改)。数字文本可以属于三种不同的数字类型:整数、浮点和复数。

Example 4: How to use Numeric literals in Python?

a = 0b1010 #Binary Literals 二进制
b = 100 #Decimal Literal  十进制
c = 0o310 #Octal Literal 八进制
d = 0x12c #Hexadecimal Literal 十六进制

#Float Literal 浮点数
float_1 = 10.5 
float_2 = 1.5e2

#Complex Literal  复数
x = 3.14j

print(a, b, c, d)  #10 100 200 300
print(float_1, float_2) #10.5 105.0

print(x, x.imag, x.real) #3.14j  3.14  0.0

在上面的代码中:

我们把整形文本分配给不同的变量。a 是二进制文本,b 是十进制文本,c 是八进制文本, d 是十六进制文本。

当我们打印变量时,所有的文本(字面量)都被转换为十进制。

10.5 和 1.5e2 是浮点型文本。1.5e2 用指数表示,相当与 1.05 * 10^2。

我们给变量x 分配了一个复数,即3.14j 。 然后我们用x.imag 和 x.real 来创建复数的 虚数 和实数 部分。

 (了解更过Python Number https://www.programiz.com/python-programming/numbers

String literals

字符串文本,是用引号括起来的有序字符。我们可以用单引号、双引号和三引号来声明字符串。

一个字符文本是一个用单引号或者双引号括起来的单个字符。

Example 7: How to use string literals in Python?

strings = "This is Python"  #字符串文本
char = "C" #字符文本
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"

print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)

结果如下:

This is Python
C
This is a multiline string with more than one line code.
Ünicöde
raw \n string

In the above program, This is Python is a string literal and C is a character literal. The value with triple-quote """ assigned in the multiline_str is multi-line string literal. The u"\u00dcnic\u00f6de" is a unicode literal which supports characters other than English and r"raw \n string" is a raw string literal.

代码中,(This is Python)是一个字符串文本,(C)是一个字符文本。

在多行字符串中指定的带有三个引号“”的值是多行字符串文本。

(u"\u00dcnic\u00f6de")是一个支持除英文以外字符的unicode文本。

(r"raw \n string")是原始字符串文本。(r 后面的是什么就是什么)。

Boolean literals

布尔文本可以有两个值:True或False。

Example 8: How to use boolean literals in Python?

x = (1 == True)
y = (1 == False)
a = True + 4
b = False + 10

print("x is", x)
print("y is", y)
print("a:", a)
print("b:", b)

运行结果为:

x is True
y is False
a: 5
b: 10

In the above program, we use boolean literal True and False.

In Python, True represents the value as 1 and False as 0.

The value of x is True because 1 is equal to True.     And, the value of y is False because 1 is not equal to False.

Similarly, we can use the True and False in numeric expressions as the value. (参与运算)

The value of a is 5 because we add True which has value of 1 with 4.

Similarly,      b is 10 because we add the False having value of 0 with 10.

Special literals

特殊文本,Python中仅仅包含一个特殊文本:None。我们使用它来指定未创建的字段

Example 9: How to use special literals in Python?

drink = "Alibaba"
food = None

def menu(x):
    if x == drink:
        print(drink)
    else:
        print(food)

menu(drink)
menu(food)

结果如下:

Alibaba
None

代码中,我们定义个一个 menu 函数。在函数内部,当参数为drink时,它显示Alibaba 。当参数为food时,它显示None。

Literal Collections

文本集合

有四种文本集合:List文本、Tuple文本、Dict文本、Set文本。

Example 10: How to use literals collections in Python?

fruits = ["apple", "mango", "orange"] #list
numbers = (1, 2, 3) #tuple
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} #dictionary
vowels = {'a', 'e', 'i' , 'o', 'u'} #set

print(fruits)
print(numbers)
print(alphabets)
print(vowels)

结果如下:

['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}

了解更多的文本集合(https://www.programiz.com/python-programming/variables-datatypes

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值