Carambola Python based tutorial - Chapter 5: Python data type (a) a variable number, string,

I CSDN blog column: HTTPS: //blog.csdn.net/yty_7
Github Address: https: //github.com/yot777/Python-Primary-Learning

 

5.1  Variable Overview

5.2  One of six data types: Digital

5.3  six data types of the two: string

 

5.1 Variable Overview

And most programming languages must first declare the variable type and then use a different , Python variable has no type. Python called "data type" is the type of objects in memory within the meaning of the variables. Variables need not be declared. Each variable must be assigned before use variable assignment after the variable will be created.

Python directly assign values to variables:

a=5
b='A'
c='Test'
print(a)
print(b)
print(c)
运行结果:
5
A
Test

Java declare a variable assignment and then type:

public class Test1 {
public static void main(String[] args) {
        int a=5;
        char b='A';
        String c="Test";
        System.out.println(a);
        System.out.println(b);   
        System.out.println(c);
        }
}  
运行结果:
5
A
Test

. 5 .2 one of the six types of data: Numbers (numbers)

It contains int (integer) , float (floating point) , BOOL (Boolean) , Complex ( plural)

#举例:
>>> a, b, c, d = 20, 5.5, True, 4+3j
>>> print(type(a), type(b), type(c), type(d))

#运行结果:
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>

Visible, Python can be assigned to multiple variables at the same time;

           Python automatically deduce the type of variable in accordance with values of the variables belonging to

#数字四则运算举例
>>> 5 + 4 # 加法
9
>>> 4.3 - 2 # 减法
2.3
>>> 3 * 7 # 乘法 
21
>>> 2 / 4 # 除法,得到一个浮点数
0.5 
>>> 2 // 4 # 除法,得到一个整数
0 
>>> 17 % 3 # 取余
2 
>>> 2 ** 5 # 乘方
32

It is seen, when calculating the mixing, the Python integer operand converted into float.

 

. 5 .3 six data types of the two: String ( String)

String in single quotation marks ( '') or double quotes ( "") enclose, while the backslash (\) escape special characters.

Note that, the Python is no separate char character type, the character is a length of a string.

#举例:
s = 'Yes, he doesn't.'
print(s)运行会报错。

s = 'Yes, he doesn\'t.'
print(s)

#运行结果:
Yes, he doesn't.

If you do not want the backslash escape occurs, you can add a string in front of r , represents the original string.

#举例1:
print('C:\some\name')
#运行结果:
C:\some
ame

#举例2:
print(r'C:\some\name')
#运行结果:
C:\some\name

The string can be + operator strings are connected together, with the * operator or repeatedly:

#举例:
print('str'+'ing', 'my'*3)
#运行结果:
string mymymy

Reference Tutorial:

Liao Xuefeng of Python tutorials

https://www.liaoxuefeng.com/wiki/1016959663602400

Liao Xuefeng's Java Tutorial

https://www.liaoxuefeng.com/wiki/1252599548343744

Python3 Tutorial | Tutorial rookie
https://www.runoob.com/python3/
 

If you feel Benpian chapter has helped you, welcome attention, comments, thumbs up! Github welcome you Follow, Star!

发布了25 篇原创文章 · 获赞 3 · 访问量 2172

Guess you like

Origin blog.csdn.net/yty_7/article/details/104120389