Python Learning Notes Day02

Article Directory variable Declare Variables Variable Naming Specification data type Six Types Number Type===Number Type Int Type===Integer Mode 1: D...
Declare Variables
Variable Naming Specification
Six Types

Article Directory

variable

Container to store data for richer storage

Declare Variables

Variable name = Data

Variable Naming Specification

  • Cannot use Chinese, English is recommended

  • Variables can use numbers but cannot start

  • Variable names cannot use special symbols, but can be underlined_

  • Case sensitive

  • Variable names need to be meaningful

  • Cannot conflict with keyword

data type

There are countless types of Python

Six Types

Number Type===Number Type

Int Type===Integer

Integer is also an integer

Keyword: int or integer

Mode 1: Decimal declaration

0~9

Sample Code

intvar = 250 print(intvar) print(type(intvar))

Run Results

250 <class 'int'>
Mode 2 binary declaration

0 1

Sample Code

intvar = 0B10001 print(intvar) print(type(intvar))

Run Results

17 (result output is decimal) <class 'int'> Mode 3: Octal declaration

0~7

Sample Code

intvar = 0o147 print(intvar) print(type(intvar))

Run Results

103 <class 'int'>
Mode 4 hexadecimal declaration

0~9 ABCEDEF

Sample Code

intvar = 0x14af print(intvar) print(type(intvar))

Run Results

5295 <class 'int'>
Float Type===Float Type
  • Floating point number is decimal

  • Keyword: float or real or double

Method 1: Declare with decimals

Sample Code

floatvar = 3.1415926 print(floatvar) print(type(floatvar))

Run Results

3.1415926 <class 'float'>
Method 2: Scientific Counting

Sample Code

floatvar = 3.5e10 print(floatvar) print(type(floatvar))

Run Results

35000000000.0 <class 'float'>
Bool Type===Boolean

There are only two values: True and False

Sample Code

boolvar = True print(boolvar) print(type(boolvar)

Run Results

True <class 'bool'>
Complex Type===Complex Number Method 1: Use an expression

Sample Code

comvar = 55 + 22j print(comvar) print(type(comvar))

Run Results

(55+22j) <class 'complex'>
Method 2: Use specific functionality

Sample Code

comvar = complex(22, 55) print(comvar) print(type(comvar))

Run Results

(22+55j) <class 'complex'>

String Type===String Type

The declaration of a string

Method 1: Single quotation mark declaration string''

Sample Code

strvar = '123' print(strvar) print(type(strvar))

Run Results

123 <class 'str'>
Method 2: Double quotation mark declaration string ""

Sample Code

strvar = "123" print(strvar) print(type(strvar))

Run Results

123 <class 'str'>
Method 3: Three quotes declare the string'''''or'''''

Sample Code

strvar = '''123''' print(strvar) print(type(strvar))

Run Results

123 <class 'str'>
Selection of String Declarations
  • If there are double quotes in the string, it is recommended to declare them in single quotes
  • Double quotation marks are recommended if there are single quotation marks in the string
  • Triple quotation marks are recommended if the string contains both double and single quotation marks.
Escape Characters: Changing the Meaning and Function of Characters

Sample Code

strvar = '1\'2\'3' print(strvar) print(type(strvar))

Run Results

1'2'3 <class 'str'>
Common escape characters \n denotes a line break symbol \r means carriage return, not the return key, back to the beginning of the current line \'denotes a single quotation mark character (no function) \"Represents a double quotation mark character (no function) \Represents a backslash (no function) \Continuation Character Good \t denotes indentation symbols

Sample Code

strvar = 'Abed, I see a silver light,\n Suspected frost on the ground' print(strvar) strvar = 'Look at the bright moon,\r Look down at your home' print(strvar) strvar = 'Look at the bright moon,\t Look down at your home' print(strvar) strvar = 'Look at the bright moon,\ //Look down at your home' print(strvar)

Run Results

Abed, I see a silver light, Suspected frost on the ground Look down at your home Looking up at the moon, looking down at home Raising my head, I see the moon so bright; withdrawing my eyes, my nostalgia comes around Metacharacter: Output in a string-defined format without escaping any escape characters

Sample Code

strvar = r'Bedside\t bright\'Moonlight\',\n Doubtfully\'Frost\'' print(strvar) strvar = R'Bedside\t bright\'Moonlight\',\n Doubtfully\'Frost\'' print(strvar)

Run Results

Bedside\tMing\moonlight\\n Suspected frost\ Bedside\tMing\moonlight\\n Suspected frost\ Enter key is not a return symbol

Under Windows platform:\r\n

On Linux platform:\n

Under Macos platform: \r Before 10.9n After 10.9

List Type===List Type

  • Ordered Containers

  • The value of a specific item can be modified

Declaration List

Sample Code

# 0 1 2 3 4 listvar = ['army officer's hat ornaments', 'Xishi', 'Yang Yuhuan ', 'Wang Zhaojun', 4] # -5 -4 -3 -2 -1 print(listvar) print(type(listvar)) print(listvar[1]) print(type(listvar[1])) listvar[2] = 'favorite concubine of emperor Minghuang' print(listvar) print(type(listvar))

Run Results

['Mink Cicada','Xishi','Yang Yuhuan','Wang Zhaojun', 4] <class 'list'> Xishi <class 'str'> ['Mink Cicada','Xishi','Yang Guifei','Wang Zhaojun', 4] <class 'list'>

Tuple Type===Tuple Type

  • Ordered Containers

  • The value of a specific item cannot be modified, and the modification will result in an error.

Tuple declaration

Specific code

# 0 1 2 3 4 tuplevar = ('army officer's hat ornaments', 'Xishi', 'Yang Yuhuan ', 'Wang Zhaojun', 4) # -5 -4 -3 -2 -1 print(tuplevar) print(type(tuplevar)) print(tuplevar[1]) print(type(tuplevar[1]))

Run Results

('Mink Cicada','Xishi','Yang Yuhuan','Wang Zhaojun', 4) <class 'tuple'> Xishi <class 'str'>

Set Type===Set Type

  • Unordered combination of a specific set of data

  • Data in a collection is not duplicated

  • Collection has no order

Collection declaration

Sample Code

setvar = {'army officer's hat ornaments', 'Xishi', 'Yang Yuhuan ', 'Wang Zhaojun', 4} print(setvar) print(type(setvar)) setvar = {'army officer's hat ornaments', 'Xishi', 'Xishi', 'Xishi', 'Yang Yuhuan ', 'Wang Zhaojun', 4} print(setvar) print(type(setvar))

Run Results

{4, 'Wang Zhaojun', 'Yang Yuhuan ', 'Xishi', 'army officer's hat ornaments'} <class 'set'> {4, 'Wang Zhaojun', 'Yang Yuhuan ', 'Xishi', 'army officer's hat ornaments'} <class 'set'>

Dict Type===Dictionary Type

  • A set of data consisting of keys and values

  • Key: character or number

  • Value: Any data type

Sample Code

dictvar = {'Pedestrian': 'Wusong', 'Little Cyclone': 'Chaijin', 'Zhiduoxin': 'Wu Yong'} # Key Value print(dictvar) print(type(dictvar)) print(dictvar['Little Cyclone']) #Value can only be checked by key

Run Results

{'Pedestrian':'Wusong','Small Cyclone':'Chai Jin','Zhiduoxing':'Wu Yong'} <class 'dict'> Chaijin A self-taught python dog Three original articles were published, 0 won and 20 visited Private letter follow

31 January 2020, 21:58 | Views: 7797

Add new comment

For adding a comment, please log in
or create account

0 comments