Python Notes Summary Week1

Source: Internet
Author: User

1. Python Introduction:

Inventor: Guido

Applications: Network applications, scientific operations, GUI programs, System management tools, other programs

Advantages: Easy to understand, high development efficiency, high-level language, portability, extensibility, embeddable.

Cons: Slow, code cannot encrypt, threads cannot take advantage of multi-CPU problems.

2. "Hello World"

Create a file named, myfirstpyprogram.py

VI myfirstpyprogram.py

#!/usr/bin/env python

Print "hello,world!"

Print "goodbye,world!"

Save after execution:

$ python myfirstpyprogram.py

hello,world!

Goodbye, world!

3. Data type

To run a program, it is necessary to describe its algorithm first. Describing an algorithm should first describe the data to be used in the algorithm, the data is described in the form of variables or constants. Each variable or constant has a data type. There are 5 basic data types for Python: integer (int), floating-point (float), character (String), Boolean (bool), null (None).

-Integer

Python can handle integers of any size, and the representation in the program is exactly the same as the mathematical notation.

-Floating point number

Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are equal. Floating-point numbers can be written in mathematical notation, such as,, 1.23 3.14 , and -9.01 so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9 , or 12.3e8 , 0.000012 can be written 1.2e-5 , and so on.

Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.

-String

strings are arbitrary text enclosed in "or", for example ‘abc‘ , and "xyz" so on. Note that the "or" "itself is only a representation, not part of a string, so the string ‘abc‘ is a only b , c this 3 characters. If it is also a character, it can be enclosed in "", for example, the "I‘m OK" containing character is I ,,, a space, which is m O K 6 characters.

-Boolean value

A Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only True False two values, either, True or, False in Python, you can directly use True and False represent a Boolean value (note case). It can also be computed by Boolean operations.

-空值

A null value is a special value in Python, None denoted by. Nonecannot be understood as 0 , because 0 it is meaningful, and None is a special null value.

Arithmetic operations

The following example a = ten, b= 20

Operator

Describe

Example

+

Addition operation

A + b get 30

-

Subtraction operations

A-B-10

*

Multiplication operations

A * b get 200

/

Division operation

B/A 2.

%

Modulo-divides the value on the left side of the% number by the value to the right of the% number and returns the remainder of the resulting result

10%5 got 0, 10%3 got 1, 20%7 got 6.

**

Power-Returns the y power of X, which is the number of times the return

2**8 256.

//

Divide-Returns the integer portion of the quotient of x divided by Y

9//2 got 4, 9.0//2.0 got 4.0.

Comparison operation

The following example a = ten, b= 20

Operator

Describe

Example

==

Determine if two objects are equal

(A = = B) is not true.

!=

Determine if two objects are not equal

(A! = B) is true.

<>

Determine if two objects are not equal

(a <> B) is true. Same as the! = operator.

>

Greater than-returns whether a is greater than B

(A > B) is not true.

<

Less than-returns whether a is less than B

(A < b) is true.

>=

Greater than or equal-returns whether a is greater than or equal to B

(a >= B) is not true.

<=

Less than or equal-returns whether a is less than or equal to B

(a <= B) is true.

Assignment operations

Operator

Describe

Example

=

Assignment-assigns the value on the right to the variable name on the left

c = A + B will assign the result of A+b to C

+=

Self-assignment-adds the value to the left of the + = number to the right of the + = number, and then assigns the result to the value around + =

c + = a equals c = C + A

-=

Self-decrement assignment

c-= a equals c = c-a

*=

Squared Assignment Value

C *= a equals c = c * A

/=

Self-removal assignment

C/= a equals c = c/a

%=

Pickup mode Assignment

C%= a equals c = c% A

**=

Self-seeking power assignment

C **= a equals c = c * * A

//=

Full assignment for pickup

C//= a equals c = c//A

4. Variables and Constants

Name = "Mo"

name2 = Name

Print (name,name2)

name = ' MS '
Print (name,name2)

Analysis:

    1. . Define name= "Mo", the interpreter creates the string "Mo" and the variable name, and points the name to "Mo"

    2. Executing Name2=name, the interpreter creates the name2 variable and points name2 to the name variable pointing to the

      The string

    3. At this point the ID built-in function to look at the two variables pointed to the memory address, the result is pointing to the same address.

    4. Execute name= "MS", the interpreter creates a new variable and changes the name to "MS"

    5. At this point, you can see the memory address of the two variables, and you will find that the point of name has become a new address.

      That is, "MS" where the memory address, but Name2 still point to the original "Mo."

5. Cycle and condition judgment-if else/if elif else/for

Login interface: Enter the user name password, both correctly print the welcome message, the correct conditions are not met the print failure notification.

user = ' Monica '
Passwd= ' Mocha '

Username = input ("Username:")
Password = input ("Password:")

If user = = Username and passwd = = password:
Print ("Welcome login")
Else
Print ("Invalid username or password..")

For loop + conditional judgment-guess age: three times to guess the exit loop; If the number is higher or lower, it will give a reminder (with If,elif,else), the third question whether to continue, is to continue the loop, do not continue to exit the loop (If,else).  

Age = 22
Counter = 0
For I in range (10):
Print ('-->counter: ', counter)
If counter <3:
guess_num = Int (input ("Input your guess num:"))
if Guess_num = = Age:
Print ("congratulations! You got it. ")
Break #不往后走了, jump out of the loop
Elif Guess_num >age:
Print ("Think smaller!")
Else
Print ("Think larger ...")
Else
Continue_confirm = input ("Does want to continue because you is stupid:")
if continue_confirm = = ' Y ':
Counter = 0
Continue
Else
Print ("Bye")
Break

counter + = 1 #counter = counter + 1

6. Making Personal Information table (%s)

Name = input ("Input your Name:")
age = Int (input ("Input your:")) #convert str to int
Job = input ("Input your Job:")

msg = "'
Infomation of user%s:
---------------
Name:%s
Age:%s
Job:%s
-------End-----
"% (Name,name,age,job)
Print (msg)

7. About obtaining passwords, writing files, modules

Getpass:prompt the user for a password without echoing:

Import Getpass
Username = input ("Username:")
Password = getpass.getpass ("Password:")
Print (Username,password)

Os:this module provides a portable the by using operating system dependent functionality.

Import OS

Os.system (' DF ')

Os.mkdir (' Yourdir ')
Cmd_res = Os.popen ("Df-h"). Read ()

Sys:this module provides access to some variables used or maintained by the interpreter and to functions that interact St Rongly with the interpreter.

Import Sys
Print (Sys.path)
# '/usr/lib/python2.7/dist-packages ' self-written modules

Python Notes Summary Week1

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.