Python first article

Source: Internet
Author: User

Python first article

1.python Introduction

Python is an easy-to-learn and powerful programming language with efficient built-in data structures and the ability to use concise syntax for object-oriented programming. Python's concise syntax and good compiler interpretation make it possible to become the language of use in most fields, such as Web development, scientific computing, artificial intelligence, system operations, Financial analysis, graphical gui, and many other areas where Python's shadow can be Seen. Since Python is so powerful, why not learn it?  Come body! Life are short, you need python!

2. Basic data types

first, There are several types of data that can be directly interpreted by computers

  (1) integer

The processing and representation of integers in Python is the same as in mathematical methods, such as 3,45,12345 and so On. We can do subtraction arithmetic for integers, such as:

3-2,3+3,8*2,3/28//3, divisible by rounding 8 3 divisible by remainder

(2) floating point number

Floating-point number is a decimal, its representation and use of the method is actually the same as the mathematical representation, but the integer and floating-point number in the Computer's internal storage is different, integer operation will only get an integer, floating-point arithmetic will only get floating point number. For example:

8/4 output:48.0/4.0 output:2.0

  (3) string

Python provides several ways to pass strings in comparison to numeric values, which can be identified using either single or double quotation marks, such as:

' ABC ' " ABC "

Sometimes our output contains quotes and other special characters, then we need to use a backslash (\) escape, for example:

' I said \'hello\ '
Output:I said ' Hello '
Or we can use single quotes to enclose double quotation marks, or use double quotation marks to enclose single quotes, even in three quotation marks, for example:
"i said ' Hello '" ' I said "hello" '
"" "I said ' Hello '" "" "

The operation of a string can be connect prompt by A + operation, such as ' hello ' + ' world ' output helloworld, or multiplication operations, such as "hello" to output Hellohellohello.

A string can use an index to access each element in a string. For example:

>>>word="helloWorld">>>word[1:4] output: Hel
Cannot use this
Word[2]= ' a '

3. About encoding

In python3, Although the default is UTF8 encoding, but in the actual work will still encounter a variety of coding problems, here is a brief introduction about the Encoding.

The first computer to use the encoding is ASCII code, it can only use eight bit bits to represent 256 characters, such as 01010001 for a character, and English characters is about 100, so the ASCII code can be enough to express. But other languages are not like english, such as Chinese it has more than 10,000 Chinese characters, and how many languages are there in the world? So the scientists invented a coding format called unicode, which puts all the languages of the world into a set of encodings. The average character uses a number of bytes in 2-4. however, This creates a problem, the original ASCII encoding of the English characters in the storage will be a few times more space, so UTF8 was born. UTF8 encoding is based on the unicode, in the spirit of saving, the Unicode characters are stored in 1-6 different size bytes.

4. Control Flow

  If statement

Its format is also very simple and understandable, similar to pseudo-code, such as

>>> x = int (input ("please Enter an integer:")) Please enter an integer:>>>ifX <0:x=0Print(' negative changed to zero ')elifx = =0:Print(' Zero ')elifx = = 1:          Print(' Single ')Else:          Print(' more ')

5. Looping Statements

  The format of the while loop is as Follows:

While Condition:
EXECUTE statement
For example:
>>> B = 0 while B <: = b+1 print(b)

 The format for the For loop is as Follows:

For I in Iteration object:
EXECUTE statement
For example:
>>> a = [' cat ', ' window ', ' defenestrate '] for in a: print

break and Continue statements, and ELSE clauses in loops

Break to jump out of the nearest level for or While Loop. The Continue is used to skip this for or while Loop. Else represents the statement that is executed when the for loop finishes the entire iteration of the object, while the loop condition is False. For example:

 Example of search primes 
>>> for n in range (2, 10for x in range (2 if n% x == print (n, ' equals ', x, ' * ', n//x" break #当进入if语句时执行 else : #当第二个for循环完成 When executing
Print (n, ' is a prime number ')
Output:

2 is a prime number

3 is a prime number

4 Equals 2 * 2

5 is a prime number

6 equals 2 * 3

7 is a prime number

8 equals 2 * 4

9 equals 3 * 3

Use of Continue

>>> for num in range (2, 10): ...

If num% 2 = = 0: ...

Print ("Found an even number", num) ...

Continue ...

Print ("Found a number", num)

Output:

Found an even number 2

Found a number 3

 Pass Statement

The pass statement indicates that nothing is done and is generally used as a placeholder. For example:

 while True:            Pass>>> calss mybody:          passdef  init ():          Pass 

6. Formatted output

What should we do when we want to output some formatted statements? We can do this:

"'Jack'
A placeholder with a%s, followed by a% plus the characters we want to format the input
%d = integer
%f represents floating-point numbers
%s represents a string

Homework

# User Login
n = 0
While N < 3:
N1 = input ("please Enter user Name:")
N2 = input ("please Enter Password:")
If N1 = = "eric" and N2 = = "250":
Print ("login Successful")
Break
Else
Print ("input error, please Re-enter")
n = n + 1
Else
Print ("login Failed")

Python first article

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.