1th Python Basics

Source: Internet
Author: User

One, variable

1. Variable definition rules

1.1. Variable names can only be any combination of letters, numbers, or underscores

1.2 The first character of a variable name cannot be a number

1.3 The following keywords cannot be declared as variable names [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' fi Nally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' tr ' Y ', ' while ', ' with ', ' yield ']


2. How to define

Hump body

Numberofstudents = 80

Underline

Age_of_oldboy = 56

Number_of_students = 80



Second, the basic data type:

1. String:

Define: ', ', ' ', ' "," a bunch of characters caused by

' Hi ', "Hello World"

Basic operation: +/*

Common methods:

' count ', = = number of occurrences of substring

' Index ', ' rindex ', = = the index position where the substring first appears, if there is no error

' Find ', ' rfind ', = = The first occurrence of a substring of the index position, if not present returns-1

' EndsWith ', ' StartsWith ', returns true at the beginning of a string or at the end of a string, otherwise false

' Format ' string formatting

' Join ',

' Split ', ' Rsplit ',


' Lower ', ' upper '

' Strip, ' Lstrip ', ' Rstrip ',

' Replace ',


' Splitlines ',


' Isalpha ', ' isdigit ', ' isalnum ',, ' islower ', ' isupper ',




2. Boolean operations


A and B | A (True) | A (False)

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

B (True) |  True | False

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

B (False)|  False | False


Not a | A (True) | A (False)

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

| False | True



3. List:

Definition: [] contains, each value is separated by commas, each value can be any type

Item No: 0-n index

-6-5-4-3-2-1

0 1 2 3 4 5 6

ages = [25, 26, 28, 25, 34, 27, 29]


Access:

To access the values in the list by index

Ages[0]

AGES[6]


Modify:

AGES[IDX] = value


Increase:

Pass

Delete:

Del Ages[idx]


1. The index used must be within the list index range

2. List_name[idx]

LIST_NUM[IDX] = value

Del List_num[idx]


Number: Len ()

Determines whether in the list in


= = List (seq)

Range (start, end, step)

Start starts every time +step, until end (does not contain end)

Start, start + step, start +2step = end


Start + N * Step

Start = 1, end = ten, step = 2


Start = 1


While True:

Print start

Start + = Step

If Start >= end:

Break



Range (start, end, step)

Range (end), start = 0, step=1

Range (start, end), step = 1



Arithmetic

+ [1, 2] + [True, False]

* ' ABC '


Slice

Get a subset of the elements from the list as a new list

Nums = Range (10)

Nums = [0, ..., 9]

Nums[start:end:step]

[Nums[start], Nums[start + step], ..., Nums[start + N * Step < end]]


Nums[0:9:2]


Nums[start:end:step]

Nums[start:end] =>step=1

Nums[start:] = End=>len (N)

nums[:]

Nums[:end]

Nums[:end:step]

Nums[::step]



nums[:]


Nums = Range (10)

NUMS2 = Nums

NUMS3 = nums[:]

Address, value

Basic data Type = = value type

A = 1

b = 1

c = b


A = []

A = b


b Change Del, update, will affect a, address delivery

Nums = Range (10)

Nums[start:end] = []


Method of List

1. Update the elements in list with no return value

2 does not update elements in list, but has a return value

3. Update the list element with a return value


A = 1

b = 2

temp = a

A = b

b = Temp


1. While

2. Raw_input

3. If

If ... else ...

If ... elif ...

If ... elif ... else ...

4. Append

5. Len () = = 0

6. Pop (0)


4. Tuples:

With () is included, separated by commas, no element, only one element, the last comma cannot be omitted

(1,)


Tuples are sequences that cannot be modified (points cannot be changed, values cannot be changed)

Address, value


MyList = []

Mytuple = (1, 2, 3, MyList, 4)

Tuple ()



Third, Process Control


If-Logical expression 0:

Child Statement 0

elif Logical expression 1:

Child Statement 1

elif Logical Expression 2:

Child Statement 2

....

Else

Child Statement N


If-Logical expression 0:

Child Statement 0

elif Logical expression 1:

Child Statement 1

elif Logical Expression 2:

Child Statement 2

....


While logical expression:

Child statements

Else

Child statements


for Var in seq:

Child statements


Range (N) = [0, ..., N-1]


For name in [' KK ', ' Woniu ']:

Print Name


For I in range (10):

if i = = 5:

Break/continue

Print I


1th Python Basics

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.