6. Python Data type

Source: Internet
Author: User

A. Integer int
-Create
A = 123
a = Int (123)
-Conversion
Age = "18"
new_age = Int (age)
B. Boolean values
-Create
A = True
b = False
-Conversion
-Number conversion, only 0 is false, other true
-String, only "" is false, other true
-To be Continued ...

C. String
-Create
A = "Alex"
A = str (' Alex ')
-Conversion
Age = 19
New_age = str (age)
-Concatenation of strings
name = ' Alex '
gender = ' female '
NEW_STR = name + gender
Print (NEW_STR)

-String formatting
# placeholder,
# name = ' My name is Li Jie, sex:%s, I'm%s this year, I'm lying! '
# NEW_STR = name% (' male ', 19,)
# Print (NEW_STR)

name = ' My name is Li Jie, sex:%s, I'm%s this year, I'm lying! '% (' male ', 19,)
Print (name)

-Determine if the subsequence is in

Content = "Alex went to Thailand to play a girl a few days ago, accidentally contracted a disease, his inner activity is, really should come a few more"

If "Go the previous day" in content:
Print (' contains sensitive characters ')
Else
Print (content)

-Remove whitespace
val = "Alex"
Print (val)
# new_val = Val.strip () # Around
# new_val = Val.lstrip () # Left
# new_val = Val.rstrip () # Right
# Print (New_val)
-Split
User_info = "Alex sb123 9"
# v = user_info.split (' | ')
# v = user_info.split (' | ', 1)
# v = User_info.rsplit (", 1)
# print (v)
-Length (character)
val = "Li Jie sb"
v = Len (val)
Print (v)
-Index
val = "Li Jie sb"
v = val[0]
Print (v)

val = input (' >>> ')
i = 0
While I < Len (val):
Print (Val[i])
i + = 1
-Slicing

name = ' My name Li Jie, gender I'm a year old, I'm lying! '
Print (Name[0])
Print (Name[0:2])
Print (Name[5:9])
Print (name[5:])
Print (Name[5:-2])
Print (name[-2:])

D. List
Create:
A = [' Alex ', ' dog ', ' Eric ', 123]
A = list ([' Alex ', ' dog ', ' Eric ', 123])

In judgment:

If ' Al ' in a:
Pass

If ' al ' in A[0]:
Pass

Index:
val = a[0]

Length:
val = Len (a)

Slice:
A = [' Alex ', ' dog ', ' Eric ', 123]
v = a[0::2]
Print (v)

Additional:
A = [' Alex ', ' dog ', ' Eric ', 123]
A.append (' Xxoo ')
Print (a)
Insert:
A = [' Alex ', ' dog ', ' Eric ', 123]
A.insert (0, ' ox ')
Print (a)
Delete:
A = [' Alex ', ' Eric ', ' dog ', ' Eric ', 123]
# a.remove (' Eric ')
Del A[0]
Print (a)

Update:
A = [' Alex ', ' Eric ', ' dog ', ' Eric ', 123]
A[1] = ' Aston sent to '
Print (a)

For loop:
A = [' Alex ', ' Eric ', ' dog ', ' Eric ', 123]
For item in a:
Print (item)
# break
# continue

E. Dictionary operations
-Create

v = {
' Name ': ' Alex ',
' Password ': ' 123123 '
}

# Common Operations

# Index Get value
# n = v[' name ']
# print (n)

# increase, none, increase; have, modify
# v[' age '] = 19
# print (v)

# Delete
# del v[' name ']
# print (v)

# loops
# for item in V.keys ():
# Print (item)
# for item in V.values ():
# Print (item)
# for Key,val in V.items ():
# Print (Key,val)


Print (len (user_dict))

PS: Mutual Nesting
User_dict = {
' K1 ': ' V1 ',
' K2 ': {' kk1 ': ' Vv1 ', ' kk2 ': ' Vv2 '},
' K3 ': 123,
' K4 ': [' Alex ', ' Eric ', [' A ', ' B ', ' C '], ' Li Jie ', {' k11 ': ' Vv1 '}],
}
user_dict[' K4 '][2].append (' 123 ')
user_dict[' K4 '][4][' n '] = ' over what dragon '


Application:

User_list = [
{' name ': ' Alex ', ' pwd ': ' 123123 ', ' Times ': 1},
{' name ': ' Eric ', ' pwd ': ' 123123 ', ' Times ': 1},
{' name ': ' Tony ', ' pwd ': ' 123123 ', ' Times ': 1},
]
user = input (' User name: ')
PWD = input (' Password: ')
For item in User_list:
if user = = item[' name '] and pwd = = item[' pwd ']:
Print (' Login successful ')
Break

6. Python Data type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.