Some ways to use Python numbers, strings, dictionaries, lists, Ganso, collections

Source: Internet
Author: User

Number: int integer effect: Age, grade, salary, ID card, QQ float float type effect: with decimal number

See self later when you don't see---------------------------------------------------

String: function: Name, gender, nationality, address, etc. description information

Definition: In quotation marks, double quotes, three quotation marks, consisting of a string of characters

Name= ' Elroy ' can only save one value

Common operations:

removing blank strip:

Name=input ("Username:")

Print (Name.strip ())

Effect: Username:eeee eeee

----------------------------------

Splitting split (cut into a list of lists):

user_info= ' Meng/xing/long/elroy ' Print (User_info.split ("/"))

Effect: [' Meng ', ' Xing ', ' long ', ' Elroy ']

----------------------------------

Length len:name= ' Elroy ' Print (len (name))

Effect: 5

----------------------------------

Index

----------------------------------

Slices (with a header without a tail in the specified range):

Name= ' Elroyy '

Print (Name[1:4])

Effect: LRO

========================================

String Other methods:

Name= ' Elroy '

------------------------------

Judge with what begins StartsWith:

Print (Name.startswith (' El '))

Effect: True

------------------------------

Judge with what end EndsWith:

Print (Name.endswith (' Roy '))

Effect: True

------------------------------

Replace Replace with string:

Name= ' Elroy Say:i has one tesla,my name is Elroy '

Print (Name.replace (' Elroy ', ' Heiheihei '))

Effect: Heiheihei say:i has one tesla,my name is Heiheihei

----------------------------------------------------------

String Increment format:

Three kinds: 1.print ("{}{}{}". Format (' Elroy ', ' ' ", ' IT ')

Effect: Elroy20it 2.print ("{0}{0}{1}". Format (' Elroy ', ' ' + ', ' IT ')

Effect: Elroyelroy20 *3.print ("{name}{age}{sex}". Format (name= ' Elroy ', sex= ' + ', age= ' IT '))

Effect: elroyIT20

--------------------------------------------------------------------------------------

Determine if the input is a digital isdigit:

Age= ' 123123 '

Print (Age.isdigit ())

Effect: True

------------------------------------

Uppercase to lowercase:

Name= ' Elroy ' Print (Name.lower ())

Effect: Elroy

----------------------------

Low-priority string handling:

Name= ' elroy123 '

Determines whether the isalnum consists of strings and numbers:

Print (Name.isalnum ())

Effect: True

--------------------------------

Determines whether the string consists of Isalpha:

Name= ' Elroy '

Print (Name.isalpha ())

Effect: True

================================================

Digital judgment:

Num=b ' 3 ' #Bytes num1=u ' 3 ' #unicode

The default in Python3 is Unicode num2= ' four ' #中文数字 num3= ' IV ' #罗马数字

------------------------------------------------

Bytes,unicode can be judged by isdigit Unicode, Chinese numerals, Roman numerals can be isnumberic judged

======================================================

The list (which can be any data type) is ordered by string and list:

Common operations: Index slices

------------------------------------------------

* Additional append:

my_friends=[' Elroy ', ' Meng ', ' Xing ', ' Long '] my_friends.append (' Heiheihei ') print (my_friends)

Effect: [' Elroy ', ' Meng ', ' Xing ', ' long ', ' Heiheihei '-----------------------------------------------

* Append insert:

my_friends=[' Elroy ', ' Meng ', ' Xing ', ' Long '] my_friends.insert (2, ' Heiheihei ') print (my_friends)

Effect: [' Elroy ', ' Meng ', ' Heiheihei ', ' Xing ', ' long ']

-----------------------------------------------

* Delete (delete by subscript) Pop:

my_friends=[' Elroy ', ' Meng ', ' Xing ', ' Long '] my_friends.append (' Heiheihei ') my_friends.pop (0)

Print (My_friends)

Effect: [' Meng ', ' Xing ', ' long ', ' Heiheihei ']

---------------------------------------

* Delete (delete by string) Remove:

my_friends=[' Elroy ', ' Meng ', ' Xing ', ' Long '] print (my_friends) my_friends.remove (' Meng ')

Print (My_friends)

Effects: [' Elroy ', ' Meng ', ' Xing ', ' long '] [' Elroy ', ' Xing ', ' long ']

-----------------------------------------

* Length len:my_friends=[' Elroy ', ' Meng ', ' Xing ', ' long ']

Print (len (my_friends))

Effect: 4

-----------------------------------------

Cycle

Contains in

Other operations (Master) Clear clear:

my_friends=[' Elroy ', ' Meng ', ' Xing ', ' Long '] my_friends.clear ()

Print (My_friends) effect: []

Copy copy:my_friends=[' Elroy ', ' Meng ', ' Xing ', ' Long '] my_friends.clear ()

Print (My_friends) l=my_friends.copy () print (L)

Effect: [] []

------------------------------------------

Counts the same string count:

my_friends=[' Elroy ', ' Meng ', ' Xing ', ' Long '] print (My_friends.count (' Long '))

Effect: 0

------------------------------------------

Add all Extend:

My_friends.extend ([' Meng ', ' Xing ']) print (my_friends)

Effect: [' Meng ', ' Xing ']

-------------------------------------------

Reverse Reverse:

My_friends.reverse () print (my_friends)

Effect: [' long ', ' Xing ', ' Meng ', ' Elroy ']

-------------------------------------------

my_friends=[' Elroy ', ' Meng ', ' Xing ', ' long ']

Print (My_friends.index ("Elroy"))

Effect: 0---------------------------------------------

Tuples (immutable lists are generally used as read-only): Indes look at the subscript location, Count view the same number age= (11,22,33,44,55) View subscript index:print (Age.index (33)) 2 View a few 33count:print ( Age.count (33)) 1

The dictionary key must be an immutable type, also known as a hash type, unordered

--------------------------------------------

* Lift a value out of the dictionary pop:

info={' name ': ' Elroy ', ' age ':, ' sex ': ' SS '}

Print (Info.pop (' name '))

Effect: Elroy

--------------------------------------------

* Viewing from a dictionary the difference between a v,get,get and a normal value is that no value returns none:

Print (Info.get (' name '))

Effect: Elroy

--------------------------------------------

Unordered Bullets k/v Popitem:

info={' name ': ' Elroy ', ' age ':, ' sex ': ' SS '}

Print (Info.popitem ())

Effect: (' sex ', ' SS ')

--------------------------------------------

Play all k and V keys, values info={' name ': ' Elroy ', ' age ': +, ' sex ': ' SS '}

Print (Info.keys ()) print (Info.values ())

Effect: Dict_keys ([' Name ', ' age ', ' sex ']) dict_values ([' Elroy ', ' SS '])

-------------------------------------------

info={' name ': ' Elroy ', ' age ': +, ' sex ': ' SS '} for Key,value in Info.items ():

Print (Key,value) Effect: Name Elroy Age-Sex SS

--------------------------------------------

Clear clear:

info={' name ': ' Elroy ', ' age ':, ' sex ': ' SS '} info.clear ()

Print (info)

Effect: {}

---------------------------------------------

info={' name ': ' Elroy ', ' age ': +, ' sex ': ' SS '} dic=info.fromkeys ([' Name ', ' age ', ' sex '],22222)

Print (DIC)

Effect: {' name ': 22222, ' age ': 22222, ' Sex ': 22222}

-----------------------------------------------

Goods = [{"Name": "Computer", "Price": 1999}, {"name": "Mouse", "Price": ten}, {"name": "Yacht", "Price": $}, {"Name" : "Beauty", "Price": 998},] Goods[0].setdefault (' AA ', []). Append (' one ') print (goods) effect: [{' name ': ' Computer ', ' Price ': 1999, ' AA ': [ ' One '}, {' name ': ' Mouse ', ' Price ': ten}, {' name ': ' Yacht ', ' Price ': $}, {' name ': ' Beauty ', ' Price ': 998}]

Set: Set de-weight and do relational operations set unordered: de-weight, relational operations (immutable types) (there can be multiple elements within a set, but each element must be immutable, i.e. not hash)

-----------------------------------------

De-weight: Elements within the collection unique S={1, ' a ', 1,1,1,1,1,1}

Print (s) Effect: {1, ' a '}

--------------------------------------------

Relational operations: s1={1,2,3,4} s2={3,4,5,6} intersection: Print (S1 & S2) {3,4} set: print (S1 | s2) Difference: print (S1-S2) print (S2-S1) symmetric difference set: Print (s1 ^ s2)

---------------------------

Unimportant: s1={1,2,3,4} s2={1,2} parent set: print (S1 >= s2) Subset: print (S1 <= S2)

Some ways to use Python numbers, strings, dictionaries, lists, Ganso, collections

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.