Python Basic Learning

Source: Internet
Author: User

Python Basics:

Strings and encodings:

In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.

When editing with Notepad, the UTF-8 characters read from the file are converted to Unicode characters into memory, and when the edits are complete, the conversion of Unicode to UTF-8 is saved to the file

In the latest version of Python 3, strings are encoded in Unicode, meaning that Python's strings support multiple languages

For the encoding of a single character, Python provides an integer representation of the Ord () function to get the character, and the Chr () function converts the encoding to the corresponding character:

Using list and tuple:

Array list

Tuple tuples and lists differ in that they are more rigorous and cannot be modified once initialized.

The so-called "invariant" of a tuple is that each element of a tuple, pointing to never change. That is, pointing ‘a‘ , cannot be changed to point ‘b‘ , point to a list, can not be changed to point to other objects, but the list itself is variable!

For example: = = "

Condition Judgment:

After Python's If judgment statement: Be careful not to write the colon less

For example:

3if age >= 18:    print(‘your age is‘, age) print(‘adult‘)else: print(‘your age is‘, age) print(‘teenager‘)

elif is Else-if's abbreviation.

 if << Execute 1>elif <<  Execute 2>elif < Conditional judgment 3>: < Execute 3>else: < execute 4>                

if statement execution features: from top to execution, as if you were to be true on a certain judgment, after executing the statement corresponding to that judgment, the remaining elif and else are ignored. So the following statement outputs teenager
20if age >= 6:    print(‘teenager‘)elif age >= 18: print(‘adult‘)else: print(‘kid‘)

Shorthand for the IF statement:
if x:    print(‘True‘)

As long as a non- x 0 value, a non-empty string, a non-empty list, etc., it is judged True , otherwise False .

Again, input () input () gets a string str, which cannot be directly compared with the integer type
birth = input(‘birth: ‘)
Python provides an int () function that converts a string to an integer type, and then compares it to an integer, but an int (' AAA ') does not, and the int() function finds that a string is not a valid number when it makes an error.
Loop statement:
Python has two loops, one of which is the for...in loop, which sequentially iterates through each element in the list or tuple, for example:
names = [‘Michael‘, ‘Bob‘, ‘Tracy‘]for name in names: print(name)
Python provides a range() function that generates an integer sequence that list() can then be converted to a list by a function, for example:
list(range(5))[0, 1, 2, 3, 4]
The second is the while loop, which, as long as the condition is satisfied, loops continuously and exits the loop when the condition is not satisfied.
0n = 99while n > 0:    sum = sum + n    n = n - 2print(sum)
Break
In a loop, the break statement can exit the loop prematurely.
Continue
continueThe effect is to end the cycle ahead of time and start the next cycle directly
breakThe statement can exit the loop directly during the loop, and the continue statement can end the cycle in advance and start the next cycle directly. These two statements usually have to be used in conjunction with the if statement.

Dict and set:
Dict is the abbreviation of dictionary
>>> d = {‘Michael‘: 95, ‘Bob‘: 75, ‘Tracy‘: 85}>>> d[‘Michael‘]95

There are two ways to avoid a key that does not exist: one is to in determine whether a key exists.
One is to determine whether a key exists by in: ' Thomas ' in D >>>false
The second is the Get () method provided by Dict, and if key does not exist, you can return none, or the value you specify:
>>> d.get(‘Thomas‘)>>> d.get(‘Thomas‘, -1)-1

set 和 dict相似
s = set([1, 2, 3])
>>s
>>{1,2,3}
add(key)添加一个元素到s中 remove(key)从s中删除一个元素

set可以看成数学意义上的无序和无重复元素的集合,因此,两个set可以做数学意义上的交集、并集等操作
>>> s1 = set([1, 2, 3])>>> s2 = set([2, 3, 4])>>> s1 & s2{2, 3}>>> s1 | s2{1, 2, 3, 4}
The only difference between set and dict is that it does not store the corresponding value, but as with the dict principle, it cannot be placed in an immutable object because it cannot be judged whether the two Mutable objects are equal, and there is no guarantee that there will be no duplicate elements inside the set

Python Basic Learning

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.