Python basic learning notes on the first day, python learning notes

Source: Internet
Author: User

Python basic learning notes on the first day, python learning notes

1. In inux and UNIX system installation (including Mac OS X), the Python interpreter already exists. Enter the python command to use

LiuyangdeMacBook-Pro :~ Liuyang $ python

Python 2.7.10 (default, Jul 30 2016, 18:31:42)

[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>

Enter the command to check whether it can run normally.

>>> Print "hello, word"

Hello, word

 

Interpreter path

#! /Usr/bin/env python

Encoding format

#-*-Coding: utf8 -*-

The encoding format of ps. python3.5 is not specified.

Python execution methods

Python interpreter py file path
Go to the interpreter in python:
Real-time input and get execution results

 

2. Numbers and expressions:

>>> 1-2
-1
>>> 1 + 2

>>> 32784*13

>>> 1/2

Try "%" to get the remainder

>>> 3, 6%

>>> 6/3

>>> 3, 6%

>>> 7/3

>>> 3, 7%

>>> 13% 9
>>> 0.75% 0.5
0.25

Try "**" power operation again

>>> 2*2*2

>>> 2 ** 3

>>> 2 ** 6

>>>-3 ** 2
-9
>>> (-3) ** 2

The 3rd power of 2 can be represented by a multiplication character (**).

 

3. Execute a user input operation

Reminder user input: user and passwordX = input ("name:") y = input ("pwd :")

Obtain the username and password, and check: username = root Password = root if x = "root" and y = "root ":
Correct: Login successful print ("success ")
Error: Logon Failed else:

Pritn ("error ")

4 variable name

The variable name can contain letters, numbers, and underscores. It cannot start with a number or use the python keyword.

5 condition statements

Indent with 4 spaces
A.
N1 = input ('>>> ')

If "alex" = "alex ":
N2 = input ('>>> ')
If n2 = "OK ":
Print ('Alex SB ')
Else:
Print ('Alex db ')
Else:
Print ('error ')


Note:
N1 = "alex" assignment
N1 = 'Alex 'comparison,
B.

If condition 1:
Pass
Elif condition 2:
Pass
Elif Condition 3:
Pass
Else:
Pass

Print ('end ')

C. Condition 1
And or

If n1 = "root" or n2 = "root ":
Print ('OK ')
Else:
Print ('OK ')

PS:
Pass refers to the blank code, meaningless, only used to represent a code block

 

6. Endless Loops 

While 1 = 1:
Print ('OK ')

The condition is always true, and the output is infinitely OK.

 

7 exercise questions

1. Use a while loop to input 1 2 3 4 5 6 8 9 10

N = 1

While n <11:

If n = 7:

Pass:

Else:

Print (n)

N + = 1

Print ("end ")

 

2. Calculate the sum of all numbers from 1.

N = 0

For I in range (101 ):

N = n + I

Print (n)

3. Output all odd numbers in 1-100

For I in range (100)

If I % 2 = 1:

Print (I)

Else:

Pass

4,

For I in range (100)

If I % 2 = 0:

Print (I)

Else:

Pass

5. Calculate the sum of all numbers of 1-2 + 3-4 + 5... 99.

N = 1

M = 0

While n <100:

Temp = n % 2

If temp = 0:

M = m-n

Else:

M = m + n

N + = 1

Print (m)

7. User Login (three chances to try again)

I = 0

While I <3:

N1 = input ("enter user name ")

N2 = input ("Enter Password ")

Print (I)

If n1 = "root" and n2 = "123"

Print ("Login successful ")

Else:

Print ("Password error, please try again ")

I + = 1

 

 

 

 

 

 


 

 

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.