Variables store values. In python, these are variable naming conventions:
Variables should start with a letter (preferably lowercase) or an underscore and can be followed by numbers or underscores
Snake case is the conventional way of writing variables, such as user_name
, with words separated by underscores (Javascript recommends camel case naming, such as: userName
)
They are case-sensitive
Keyword variables cannot be named directly (see the official document "Python Keywords")
So what will happen if you really make this mistake? For example:
>>> if = 1 SyntaxError: invalid syntax >>> print(and) SyntaxError: invalid syntax >>> def = "hi" SyntaxError: invalid syntax
Do you see it? Prompt syntax errors directly.
In addition to not using keywords as identifiers, as we have emphasized before, identifiers with the same name as built-in functions cannot be used. Python has many built-in functions. Take sum as an example. This is a summation function. Let’s take a look at the consequences of incorrect naming:
>>> sum([1,2,3,4]) 10 >>> sum = "错误标识符名" >>> sum([1,2,3,4]) Traceback (most recent call last): File "<pyshell>", line 1, in <module> sum([1,2,3,4]) TypeError: 'str' object is not callable</module></pyshell>
Ignore sum([1,2,3,4]) The meaning of it is to add 1/2/3/4 together to get 10, and then mistakenly give a variable the identifier name of sum, and then call sum([1,2,3, 4]), the program throws an exception. The reason for the error is that str is not a callable type. In the final analysis, this is the reason why the name sum is duplicated.
Simply put, a data type is a way to represent a value. In our real world, we have letters, numbers, and symbols as different types of common values. Likewise, Python contains the following basic data types:
int (integer)
float (floating point number)
str (string)
bool (boolean)
objectalso Is a non-primitive type)
Number and BigInt.
type Function is used to determine the type of a value or expression. (Similar to the
typeof operator in JavaScript)
num = 100 # variable assignement print(type(num)) # <class> num2 = 99.99 print(type(num2)) # <class> expression1 = num * 10 print(type(expression1)) # <class> expression2 = num + num2 print(type(expression2)) # <class></class></class></class></class>
In Python, variable assignment is achieved by writing the name and assigning the value using theMathematical calculationsFor mathematical calculations, in addition to the simple addition, subtraction, multiplication, and division mentioned earlier, more scientific calculations need to import=
operator of.
In JavaScript, variable names need to start with the
var,
constor
letkeywords.
mathThis Library, which contains most of the scientific calculation functions we may need, as shown in the table below. There are some built-in math functions that allow us to calculate various mathematical operations easily.
function | Return value (description) |
abs(x) |
Return number The absolute value, such as abs(-10) returns 10 |
ceil(x) |
Returns the upper limit of the number Enter an integer, such as math.ceil(4.1) returns 5 |
##exp(x) | Returns x of e Power (ex), such as math.exp(1) returns 2.718281828459045 |
returns The absolute value of the number, such as math.fabs(-10) returns 10.0 | |
returns The rounded down integer of the number, such as math.floor(4.9) returns 4 | ##log(x) |
such as math.log(math.e) returns 1.0, math.log(100,10) returns 2.0 |
##log10(x) |
Returns the logarithm of x based on 10, such as math.log10(100) returns 2.0 | ##max(x1, x2,. ..) |
min(x1, x2,...) |
|
modf(x) |
|
pow(x, y) |
|
round(x [,n]) |
|
sqrt(x) |
|
acos(x) |
Returns the arc cosine of x in radians. |
asin(x) |
Returns the arcsine radians value of x. |
atan(x) |
Returns the arctangent radian value of x. |
atan2(y, x) |
Returns the arctangent of the given X and Y coordinate values. |
cos(x) |
Returns the cosine of x in radians. |
hypot(x, y) |
Returns the Euclidean norm sqrt(xx yy) |
sin(x) |
Returns the sine value of x radians. |
tan(x) |
Returns the tangent of x in radians. |
degrees(x) |
Convert radians to angles, such as degrees(math.pi/2), Return 90.0 |
##radians(x) | Convert angles to radians |
常量 |
描述 |
pi |
数学常量 pi(圆周率,一般以π来表示) |
e |
数学常量 e,e即自然常数(自然常数)。 |
下面是一些应用展示,注意最后的角度调用方式:
>>> math.log(2) 0.6931471805599453 >>> math.cos(30) 0.15425144988758405 >>> math.cos(60) -0.9524129804151563 >>> math.sin(30) -0.9880316240928618 >>> math.sin(math.degrees(30)) -0.4097717985741408 >>> math.sin(math.radians(30)) 0.49999999999999994
字符串是Python中最常用的数据类型之一,使用单引号或双引号来创建字符串,使用三引号创建多行字符串。字符串要么使用两个单引号,要么两个双引号,不能一单一双!Python不支持单字符类型,单字符在Python中也是作为一个字符串使用。
字符串是不可变的序列数据类型,不能直接修改字符串本身,和数字类型一样!Python3全面支持Unicode编码,所有的字符串都是Unicode字符串,所以传统Python2存在的编码问题不再困扰我们,可以放心大胆的使用中文。
name = 'Python' # string assignment within single quotes name2 = "Python" # string assingment within double quotes name3 = '''This is a a very long string and supports multiline statements as well''' # string assingment within 3 single quotes name4 = 'Hello! \"Rockstar Programmer\"' # string with escaped character sequence print(type(name)) # <class> print(type(name2)) # <class> print(type(name3)) # <class> print(type(name4)) # <class></class></class></class></class>
>>> var1 = 'Hello World!' >>> var2 = "Jack" >>> var3 = "" # 空字符串 >>> var4 = "it's apple" # 双引号中可以嵌套单引号 >>> var5 = 'This is "PLANE"!' # 单引号中可以嵌套双引号 >>> var6 = 'what is 'your'name' SyntaxError: invalid syntax
但是单引号嵌套单引号或者双引号嵌套双引号就会出现歧义。
>>> a = "haha" >>> b = a >>> a = "xixi" >>> a is b False >>> a[1] = "z" Traceback (most recent call last): File "<pyshell>", line 1, in <module> a[1] = "z" TypeError: 'str' object does not support item assignment</module></pyshell>
虽然字符串本身不可变,但可以像列表序列一样,通过方括号加下标的方式,访问或者获取它的子串,当然也包括切片操作。这一切都不会修改字符串本身,当然也符合字符串不可变的原则。
>>> s = "hello world!" >>> s[4] 'o' >>> s[2:6] 'llo ' >>> s 'hello world!'
与 Javascript 类似,可以使用+
运算符连接字符串。它只是连接或“连接”字符串。
first_name = 'Mike' last_name = 'Tyson' print(first_name + ' ' + last_name) # Mike Tyson
与存在隐式类型转换(又名强制类型转换)的 Javascript 不同,如果使用不同类型执行操作,Python 将抛出错误:
user_name = 'John' age = 40 print(user_name + age) # TypeError: can only concatenate str (not "int") to str # This would work in Javscript where it would convert the result to string type
在 Python 中,需要显式转换类型才能执行不同类型的操作:
user_name = 'John' age = 40 print(user_name + str(age)) # John40 print(type str(age)) # <class></class>
同样,字符串也可以转换为数字:
lucky_number = 7 lucky_number_stringified = str(7) lucky_number_extracted = int(lucky_number_stringified) print(lucky_number_extracted) # 7
The above is the detailed content of Python data type introduction example code analysis. For more information, please follow other related articles on the PHP Chinese website!