C language Old driver learn Python (ii)

Source: Internet
Author: User

Standard data types:

Total 6 types: number (numeric), string (string), list, tuple (tuple), sets (collection), Dictionary (dictionary)

This study is mainly with the data type mixed face familiar, know what each thing do, what characteristics. The method of specific usage and data type, check the reference book on the line.

Line up, one by one.

Digital:

More simple than C, there are only 4 types of numbers, namely int, float, bool, complex, Nani? There are plural.

int is a long integer, and there is no short integer in the C-word. See, with the powerful computer hardware, the past invaluable memory resources are no longer rare, how to the programmer convenient and friendly how to come, this is also a basic design of modern language ideas.

You can use Type () and isinstance () to know the type of the object to which a variable is pointing. The difference is that type () is more dead-minded, but isinstance () considers the subclass to be a parent class type. That is: isinstance (subtype) = = Parent Type returns True

Since it mentions true and false, let's say, Python3, true and false are the keywords, with values of 1 and 0, respectively. You can try print (true+1), the result is 2.

In the case of numeric operators, most are similar to C, and a few are not the same, but, to be sure, they are more convenient. For example, the result of 2/4 is that 0.5,python is automatically calculated as a floating-point number. This is written in C is equal to 0, to want to get 0.5 also forced conversion type. So, if it is to take the whole what to do, can be written 2//4, the result is 0. As an operator, it also determines that it cannot be used as a single-line comment as in C.

The exponentiation is a**b, and it is not necessary to call another function library like C.

String:

The string is enclosed in single quotation marks (') or double quotation marks ("). I like single quotes because you don't have to press shift to be quick and efficient.

In the usual way, \ means escape, and if you increase the write or lowercase r before the string, it is no longer escaped.

String concatenated with the + sign, copy the string n times with the * number.

The string intercept is in the form of a variable [subscript 1, subscript 2], subscript index starting at 0, and the end position as-1, which is a left-closing interval, [).

Sample Quick check:

str = ' Runoob '

Print (str) # output String Runoob
Print (str[0:-1]) # outputs all characters from the first to the penultimate Runoo
Print (str[0]) # output string First character R
Print (Str[2:5]) # Output from the third start to the fifth character Noo
Print (str[2:]) # outputs all characters from the beginning of the third noob
Print (str * 2) # output string two times Runoobrunoob
Print (str + "TEST") # Connection string runoobtest

You cannot give an assigned value to a single character in a string, str[1]= ' A ' will make an error.

In addition, Python does not have the C language character type char.

Fully understandable. Said before, what time, also save what memory ah, how convenient how to come.

List:

The definition of the list is ListA = [x, xx,xxx], square brackets wrapped, comma separated, at first glance like the C language of the array, but in fact not the same. From the Append (), pop () and other methods, but also like a Java inside the array. Let me just say that the list in Python is a hybrid-enhanced version of the array bar, the maneuverability is almost inverse of the sky. See Example:

list = [' ABCD ', 786, 2.23, ' Runoob ', 70.2]
Tinylist = [123, ' Runoob ']

Print (list) # output complete list [' ABCD ', 786, 2.23, ' Runoob ', 70.2]
Print (list[0]) # output list first element ABCD
Print (List[1:3]) # Output from the second start to the third element [786, 2.23]
Print (list[2:]) # Outputs all elements starting from the third element [2.23, ' Runoob ', 70.2]
Print (Tinylist * 2) # output two-time list [123, ' Runoob ', 123, ' Runoob ']
Print (list + tinylist) # connection list [' ABCD ', 786, 2.23, ' Runoob ', 70.2, 123, ' Runoob ']


And the string can not change the elements of the different, the list of changes, and it is simply a casual change, not too convenient.

A = [9, 2, 13, 14, 15, 6]
A[2:5] = [] # sets the corresponding element value to []
Print (a) # The result is [9, 2, 6]

Tuple (tuple)

I have never seen this thing before. Specifically to search for a bit, the word tuple seems to be unique to Python, unlike lists and other words in life there are other meanings. Had to watch the tutorial carefully.

Explained in the tutorial:

Tuples are written in parentheses (()), and elements are separated by commas, and only one element is followed by commas.

Tuple (tuple) is similar to a list, except that the elements of a tuple cannot be modified.

A similar list? All right, turn the page.

Collection:

A sequence of unordered, non-repeating elements used for membership testing and for removing duplicate elements.

Emphasis: 1) disorder; 2) do not repeat.

Create a collection with the {} or set () function. SetA = {' Hatemath '} or SetA = Set (' Hatemath ')

Note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.

Common set operations are: set |, Difference--, intersection &, and element set that does not exist ^ (as I understand it is the set minus the intersection)

Example:

A = set (' Hate ')
b = Set (' Math ')

Print (A | b) # {' t ', ' m ', ' a ', ' e ', ' H '}
Print (A-B) # {' E '}
Print (A & B) # {' A ', ' t ', ' h '}

Print (a ^ b) # {' m ', ' e '}
Print ((a | b)-(A & B) # {' E ', ' m '} is exactly what I understand.

Collection, very good very strong. Good bye.

Hungry ... 15 years ago, when beginners to programming, often go to the computer center, in order to debug often miss food points, and put the change out. Get off the machine and run to dinner with the boss (there should be a smile on your face). After so many years still like programming, it seems to be true love.

It's also possible that I didn't think of it as a means of earning a living.

Dictionary:

A dictionary is a collection of unordered objects in which the elements are accessed by key (key) and are called values (value). Familiar! There's this thing in Java. I found that when I learned these advanced features, I had nothing to do with C.

As an acquaintance, look directly at the example.

Dict = {}
dict[' One ' = "1-Beginner's tutorial"
DICT[2] = "2-Rookie Tool"

tinydict = {' name ': ' Runoob ', ' Code ': 1, ' site ': ' www.runoob.com '}

Print (dict[' one ') # output key is ' one ' value 1-Beginner Tutorial
Print (dict[2]) # The value of the output key is 2 2-Rookie tool
Print (tinydict) # output full dictionary {' name ': ' Runoob ', ' site ': ' www.runoob.com ', ' Code ': 1}
Print (Tinydict.keys ()) # Output all keys Dict_keys ([' Name ', ' site ', ' Code '])
Print (Tinydict.values ()) # Outputs all values dict_values ([' Runoob ', ' www.runoob.com ', 1])

C language Old driver learn Python (ii)

Related Article

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.