python day01

1. Development language:
high-level language: Python Java, PHP C# Go ruby ​​C++... ---> convert to byte code
Low-level language: C, assembly ------> convert to machine code

2. Comparison between languages:
  PHP class: suitable for writing web pages, with limitations
  Python Java: can write web pages or background functions

  Comparison of Python and Java:
  1) Python has low execution efficiency and high development efficiency
  2) Java has high execution efficiency and low development efficiency

 

3. Types of Python:
JPython
IronPython
JavaScriptPython
RubyPython
CPython **********
...
pypy This is Python developed with CPython

4. Installation of python:

Python is installed on the OS, and the operation is
performed:
write a file in accordance with the rules of python, hand the file to the Python software, read the content in the file, then convert and execute, and finally obtain the result.
Python software ==> Python interpreter (memory management)
download:
Python3 continues to update Python2 and continues
to update
Window: click
python2
python3
# Environment variables
With environment variables: C:\Program Files\Microsoft Games\Solitaire
>>>Solitaire
C: \Program Files\Microsoft Games\Solitaire\Solitaire
Linux:
python2
python3

5. Python Basics

- Basic
1) The first sentence python
- The suffix name can be arbitrary
- When importing a module, if it is not a .py file ==> The file suffix name is .py
2) Two execution methods
python interpreter py file path
python entry Interpreter:
real-time input and get the execution result

3) Interpreter path
#!/usr/bin/env python
4) Encoding
# -*- coding:utf8 -*-

ascill 00000000

& 00000001


unicode 0000000000000000+

& 0010000000000001111100
in 001000000001

8 How much can be expressed is how much can be expressed
&
001000000000000111110010 in 00000001

Python3 no need to pay attention to Python2
As long as Chinese appears in each file, the header must be added

5) Perform an operation
to remind the user to enter: user and password
to obtain the user name and password, detection: user name =root Password=root Correct
: Login successful
Error: Login failed

6) Variable names
can be letters, numbers, and underscores;

Note: (1) Numbers cannot start

   (2)不能是Python的关键字:'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'

   (3) It is best not to duplicate the built-in things in python, and there will be prompt information when programming with Pycharm; 

     (4) The variable name taken can be related to the actual, which is convenient for the understanding of the code.

7) Comments in python

  (1) Single-line comment # User_id=22

  (2) Multi-line comments *** Comment content ***

8) Conditional Statements

  (1) if basic statement

  if condition:

         inner code block

    inner code block

        else:

    ......

       printf(‘........’)

Note: There must be indentation under the if condition. The same indentation represents a code block, usually 4 indentations. Press tab to execute.

example:

if 1 == 1:
     print("Welcome to the first clubhouse 1")
     print("Welcome to the first clubhouse 2")
else:
   print("Welcome to the first clubhouse")

(2) if supports nesting

if 1 == 1:
     if 2 == 2:
    print("Welcome to the first club 1")
    print("Welcome to the first club 2")
  else:
              print('Welcome to Tokyo Special ')
else:
    print("Welcome to a book")
(3) if elif

inp = input('Please enter the membership level:')

if inp == "Premium Member":
  print('Beauty')
elif inp == "Platinum Member":
  print('Big Morgan')
elif inp == "Platinum Member":
  print('First-line star')
else:
  print('city management')

print('Start service....')
Note: elif condition: elif (equivalent to else if in C language)

Supplement: pass means pass, nothing is executed, it means code block, meaningless,
  if 1==1:
  pass
  else:
  print('sb')

8) Basic data types

String (quotes): only the following 4 cases
name = "I am an essay"
name = 'alex'
name = """alex"""
name = '''I am an essay'''

Addition:
n1 = " alex"
n2 = "sb"
n4 = "db"

n3 = n1 + n2 + n4

Multiplication: equivalent to 10 repetitions of alex

n1 = "alex"
n2 = n1 * 10

Number:
age = 13

Addition, subtraction, multiplication and division to the power:
a1 = 10
a2 = 20

a3 = a1 + a2

a3 = a1 - a2

a3 = a1 * a2

a3 = 100/10

a3 = 4**4 4 to the 4th power

a3 = 39 % 8 # Get the remainder after dividing 39 by 8

Supplement:
a3 = 39 // 8 take the quotient


a = 13
temp = a % 2
if temp == 0:
print ("even")
else:
print('odd')

9) Loop

Infinite loop

while 1==1:
print('ok')

10) Practice

Knowledge points used

(1) if conditional statement
(2) while loop
(3) odd even number


1, use 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 = n + 1
print('----end----')

2. Find the sum of all numbers from 1 to 100
n = 1
s = 0
while n < 101:
s = s + n
n = n + 1
print(s)

3. Output all odd numbers within 1-100

n = 1
while n < 101:
temp = n % 2
if temp == 0:
pass
else:
print(n)
n = n + 1

print( '----end----')

4. Output all even numbers within 1-100

n = 1
while n < 101:
temp = n % 2
if temp == 0:
print(n)
else:
pass
n = n + 1

print('----end----')

5. Find the sum of all numbers from 1-2+3-4+5 ... 99
n = 1
s = 0 # s is the sum of all previous numbers
while n < 100:
temp = n % 2
if temp == 0 :
s = s - n
else:
s = s + n
n = n + 1
print(s)




Guess you like

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