. Net programmer Python path --- Python basics, python --- python

Source: Internet
Author: User
Tags python list

. Net programmer Python path --- Python basics, python --- python

Recently, I am curious about dynamic languages. So I chose the Python language.

  I. Python basics:

1. print outputs data to Python. # The output result is followed by the output result of teammates. The character is transcoded using encoded. ord obtains the ASCII, % s, and % d of the character, which is similar to the output of C language, string and integer, respectively.

Print ('Hello World ') print 100 + 200 #300 print 1> 2 # falseprint 1> '2' # false # print 'abc' + 2 # print ord ('A ') #65 print u 'xiao Cong '. encode ('utf-8') print 'name: % s, Age: % d' % ('Frank ', 23) # Name: Frank, Age: 23 (% d-> int, % f-> float, % s-> string, % x-> OXH)

Ii. usage of the Python set:

All those who have learned C # and Java know that List, Dictionary, and ArrayList in these two languages are very common. In fact, there are also corresponding List and Dic in Python.

1. list usage. the List in Python is represented by []. The List can contain different types of data, which can be understood as List <Object> in C #. The List here is different, for example, friendList [-1] actually returns friendList [Count-1], which can be simply understood as a ring with the range of-Count <= N <Count. The Python List also supports the sort (), append (), and Remove () methods,

# List usage friendList = ['frank', 'loch', 'hard']; print friendList # ['frank', 'loch', 'hard'] print friendList. count # 3friendList. append (1) print friendList; # ['frank', 'loch', 'hard', 1] # If the type is, it is equivalent to List <object> print friendList [-1] #1 print friendList [-2] # HardyfriendList in C. pop (); print friendList; ['frank', 'loch', 'hard']

2. Tuple usage. The Tuple in Python is represented by (). Once the Tuple data is declared, it cannot be modified. The data is read-only.

# Tuple usage: friendTuple = ("Frank", "Loch", "hard") friendTuple = (, []); # tuple data cannot be modified

3. dictionary usage. the Python dictionary is represented by {}. Unlike C #, if multiple identical keys exist during Declaration, only the last one is saved. del is used to delete the dictionary. personInfo [keyName] is used to add or modify the dictionary. if the keyName exists, modify the data. If the keyName does not exist, add it.

# Dictionary personInfo = {'frank': 23, 'hard': 24, 'loch': 26, 'loch': 25} print personInfo ['loch'] #25, overwrite the previous one. personInfo ['Victor '] = 23 print personInfo # {'frank': 23, 'loch': 25, 'Victor ': 23, 'hard': 24 ,} del personInfo ['Victor '] print personInfo # {'frank': 23, 'loch': 25, 'hard': 24 ,}

4. Set usage, excluding List containing the same element

#SetmySet = set([1,2,3,3,3])print mySet  #set([1,2,3]);

  

  3. Python loop and judgment.

In Python, code segments such as C # and java are not extended by {}. For Python, brackets are implemented by using the: sign.

Note: print and if are not in the same column. In fact, print is the sub-code of the if column, which belongs to {} of if. I don't know why I want to design it like this, but I am used to the brackets in C #. I really feel that braces are more conducive to code readability.

# Condition age = 10; tupe usage if age> 60: print 'Elder 'elif age> 18: print 'Adult' else: print 'minor '# cyclic numList = [1, 2, 4] for val in numList: print val

Conclusion: The above is also the most basic syntax of Python. I believe that C # basics can be easily understood. The language must be used frequently to become proficient, however, the younger brother is only interested in learning. I mainly focus on Python network programming. I heard that Python network programming is still awesome (web crawler can be used). I don't know if this is the case. learn more first. I personally think it is necessary to master at least one dynamic language. learn about the differences between different languages.

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.