Python Basics-Basics
1. The first python-suffix name can be arbitrary?
-When importing a module, if it is not a. py file
==> after the file suffix name is. py
2. Two modes of execution
Python interpreter py file path
Python enters the interpreter:
Real-time input and get to execution results
3. Interpreter Path
#!/usr/bin/env python
4. Encoding
#-*-Coding:utf8-*-
Ascill 00000000 (8-bit)
Unicode 0000000000000000+ (16-bit)
How many utf-8 can be expressed by how many means
Note:
Python3 no concern
Python2 each file appears in Chinese, the head must be added
Bytes, bits
Unicode UTF8 GBK
Utf8:3 (a Chinese occupies a few bits)
Gbk:2
5. Perform an action
Reminder user input: User and password
Get user name and password, detect: Username =root password =root
Correct: Login Successful
Error: Login failed
6. Variable Name
-Alphabet
-Digital
-Underline
Note:
Number cannot start
cannot be a keyword
It's best not to repeat the python built-in stuff.
7. Conditional statement (indent with 4 spaces)
A.
#!/usr/bin/env python
N1 = input ('>>>') if "Alex"=="Alex": N2= Input ('>>>') ifN2 = ="Confirm": Print('Alex SB') Else: Print('Alex DB')Else: Print('Error') Note: N1="Alex"Assignment N1=='Alex'Comparison
Description
if condition 1: Pass elif Condition 2: Pass elif Condition 3: Pass Else : Pass Print ('end')
C. Conditions
and or ifN1 = ="Alex" orN2 = ="alex!23": Print('OK') Else: Print('OK') PS:Passsubstituting null code, meaningless, just to represent a block of codeContinue: Terminates the current loop, starting the next loop Break: Terminates the usage of all loop input, waits forever until the user enters a value, assigns the input value to a thing
8. Cycle
dead Loop while 1==1: print('ok')
9. Basic data types
String: N1 ="Alex"N2 ='Root'N3 ="""Eric"""n4=" "Tony" "Number:age=21 weight = Fight = 5subtraction, etc.: string: addition: N1="Alex"N2="SB"N4="DB"N3= n1 + N2 +N4#"Alexsbdb"Multiplication: N1="Alex"N3= N1 * 10numbers: N1= 9N2= 2N3= n1 +n2 N3= N1-n2 N3= N1 *n2 N3= N1/n2 N3= N1%n2 N3= N1 * *N2
Exercise: Determine whether a number is an even or a number of KikiNum= 12N= num% 2ifn = =0:Print('even') Else: Print('Odd')
Exercises:
If condition statement
While loop
Odd even
1. Use while loop input 1 2 3 4 5 6. 8 9 10
# !/usr/bin/env pythonn = 1 while n < one: if n = =7 :Pass Else: print(n) = n + 1print(' ----End---- ')
2. For all numbers of 1-100
#!/bin/usr/env python
n = 1= 0 while n < 101: = s + n = n + 1print(s)
3. All odd numbers in output 1-100
#!/usr/bin/evn python
n = 1 while n < 101: = n 2 if temp = = 0: pass else
: print(n) = n + 1print('----END---- ')
4. All even numbers in output 1-100
#!/usr/bin/env python
n = 1 while n < 101: = n 2 if temp = = 0: print(n)
Else: pass = n + 1print('---- End----')
5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
n = 1# S is the sum of all previous numbers while n <: = n% 2 if temp = = 0: = S- n else: = s + n = n + 1print(s)
6. User Login (three chance retry)
#!/bin/usr/env python
Count =0 whileCount < 3User= Input ('>>>') PWD= Input ('>>>') ifuser_id = ="Root" andUser_pass = ="Root": Print "is OK" Break Else: Print('user or pass is error') Count= Count + 1
Python 1 day