Basic knowledge of python finishing 1

Language type:

One: Interpretation type: from top to bottom line by line interpretation converted into binary

      Advantages: fast development efficiency, cross-platform.

       Disadvantage: slow execution

       Compiled: All compiled and converted into binary from top to bottom

      Disadvantages: slow development efficiency, not cross-platform.

      Advantages: fast execution

Two: python types

      cpython jpython other languages ​​python

Three: python version (2.7/3)

Difference between python2 and python3:

One: a lot of repetitive code in python2, and other language bad habits.

       python3 is simple, beautiful and clear

Two: python2 prints without parentheses, python3 prints with parentheses

 

Four: Variables:

Rules: 1 Variables consist of any combination of numbers, letters, and underscores

          2 cannot start with a number

          3 cannot be a keyword in python

          4 Variables are descriptive

          5 cannot be Chinese

          6 Camel case and underline

          7 Variable names should not be too long

Five: Constants: Constants that will not change all the time, such as the founding date, festivals

Six: User interaction: input

Three logins:

count=0
while count<3:
    count = count + 1
    name = input('>>>Please enter your username')
    password = input('>>>Please enter your password')
    if name=='zhujun'and password=='12345678':
        print ('Login successful')
        break
    else:
        print('Please re-enter')

Seven: Notes

Single line comment: #

Multi-line comments: '''' or """"

Eight: if conditional statement

if condition:

        result

if and else collocation

if/elif/elif/else

Nine: while loop

Small exercise:

1. Use the while loop to input 1 2 3 4 5 6 8 9 10
count=0
while count<10:
    count=count+1
    if count==7:
        print('')
        continue
    else:print(count)

2. Find the sum of all numbers from 1 to 100
count=1
sum=0
while count<101:
    sum=sum+count
    count=count+1
print(sum)
3. Output all odd numbers within 1-100
count=0
while count<101:
    count=count+1
    if count % 2 == 1:
        print(count)
4. Output all even numbers within 1-100
count = 0
while count < 101:
    count = count + 1
    if count % 2 == 0:
        print(count)

5. Find the sum of all numbers 1-2+3-4+5 ... 99
The first:
count=1
sum=0
while count<100:
    if count%2==0:
        sum=sum-count
        count = count + 1
    else:
        sum=sum+count
        count = count + 1
print(sum)

#The second:
sum=0
for i in range(1,100):
    if i %2==0:
        sum=sum-i
    else:
        i % 2 == 1
        sum = sum + i
print(sum)

 

Sum of all odd and even numbers from 1 to 100
print(sum([i for i in range(101) if i%2==0]))
print(sum([i for i in range(101) if i%2==1]))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325016956&siteId=291194637