python learning day1

 


Today's content:
1. Introduction to Python

2. Python version

3. Hello World

4. Coding

5. Input and output

6. Variables

7. Initial data types

8. Conditional statements

9. Loop statements

10. Common data types


Details:
1. Python introduction

- Why write code? One hundred money buys a hundred chickens
for 100 cents and 100 chickens for
5 cents -g
3 cents -m
1/3 cents -x

to find a programmer to develop a software
# Question: 5 cents for a rooster, 3 cents for a hen, a chick 3 for one penny, buy 100 chickens for 100 penny, including roosters, hens, and chicks must have, ask how many roosters, hens, and chicks to buy just to make up for 100 pence?
"""
5*rooster < 100
3*hen < 100
1*chick < 300
rooster+hen+chick = 1000
"""
for g in range(1, 21):
for m in range(1, 34 ):
for x in range(1, 301):
score = g * 5 + m * 3 + x / 3
if score == 100 and g + m + x == 100:
print('The rooster is %s, the hen is %s, the chick is %s' % (g, m, x))

The purpose is to solve the problems in life.

- Compiler Interpreter
Install Python Interpreter: + Write Code That Interpreter Knows

Install Java Interpreter: + Write Code That Interpreter Knows

Install PHP Interpreter: + Write Code That Interpreter Knows

- Programming Language?
C
Python
Java
PHP
C#

- C and Others
- Machine Code: C
- Bytecode: Other

- Compiled and Interpreted
- Python, PHP
- C#/Java/C

- Difficulty
- C
- C#, Java
- PHP
- Python (complete class library/module)

summary:
1. Install the interpreter
2. Learn the language rules
3. Write code
4. The interpreter runs (interpretation)

- install the interpreter
- cpython interpreter (*)
- jpython interpreter
- ironPython interpreter interpreter
- rubyPyhton interpreter
.....
- pypy interpreter

2. Python interpreter version: cpython interpreter
- Python2.7
- Python3.6

Environment variables:
Python configuration:
;C:\Python36;
Terminal: python
pip configuration:
;C:\Python36\Scripts
Terminal: pip3 install xxxx

;C :\Python36;C:\Python36\Scripts;


3. Write a program to
create an arbitrary file. py
print('hello world')

4. Encode
ascii: use 1 byte = 8 bits to represent everything that a computer can express.
2**8 = 256
00000000 -> A
00000001 -> B
00000010 -> C
00000011
00000100
unicode: Universal code, use 4 bytes = 32 bits for the corresponding relationship
2**32 = 4294967296
00000000 00000000 0000000 -> A
0000000010-> B
00000000 0000000010-> C 00000000 10000000 00010000
00011010 -> Purple
UTF-8: Compressing the national code Section = 24 bits gbk: Correspondence to the text of Asian countries PS: Chinese 2 bytes = 16 bits Phenomenon:











py2: The default encoding of the interpreter is ascii
# -*- coding:utf-8 -*- The default encoding of the interpreter is utf-8
print('Wang Ziwei')

py3: The default encoding of the interpreter is utf-8
print('Going to sleep')


py2 /py3
# -*- coding:gbk -*-
print('going to sleep')

5. IDE
- pycharm
- vim

use:
1. Start Pycharm: select an existing interpreter

2. Run

3. Configure
- text size
- template

6. Input and output
Output :
print("You are the wind and I am the sand")
Input:
user = input("Please enter the user name:")

Password encryption:
import getpass

pwd = getpass.getpass("Please enter the password: ")
7. Variables

Format : variable name = value
Specification:
a. Numbers, letters, underscores
b. Cannot start with numbers
c. Cannot use Python keywords
Suggestion: see the name; user_pwd = "xxx"


Note:
Example 1:
name = 'alex'
user = 'alex'

Example 2:
name = 'alex'
user = name

8. Data type

age = 18 # Integer type
name = "alex" # String type

xx = "18"
xx = '18'
xx = '''18'''
xx = """18"""
xx = "123'
9. Conditional statement

Format 1:
if condition:
go here after success

Format 2:
if condition:
go here after success
else:
go here after failure

Format 3:
if condition:
go here after success
elif condition:
go here after success
elif condition: go here
after success
else:
all of the above failed

practice questions: 10086 reminder
msg = """
welcome to call 10086
1. Check the phone bill
2. Check the water meter
3. Manual service
"""

print(msg)

choice = input('Please select the service you need')
if choice == "1":
print("1. Check this machine; 2. Check other people's mobile phones; 3. Check landlines ")
search_type = input('Please enter the type to be queried:')
if search_type == '1':
print('Query this computer')
elif search_type == '2':
print('Query other people's mobile phones')
elif search_type == '3':
print('Query landline')
else:
print('Query type input error')
elif choice == "2":
print("查水表")
elif choice == "3":
print("human service")
else:
print("input error")

Problem youth:
Error:
print('Welcome to login')
user = input('Please enter user:')
Correction:
print('Welcome to login')
user = input('Please enter user:')

Girl:
Error:
msg = " ""
Welcome to call 10086
1. Check the phone bill
2. Check the water meter
3. Manual service

"""
print(msg)

choice = input("Please select the service you need")
if choice == "1":
print("1. Check this machine; 2. Check other people's mobile phones; 3. Check the landline")
search_type = input("Please enter the desired The type of query: ")
if search_type =='1':
print('Query this machine')
elif search_type == input('2'):
print('Query other people's mobile phones')
elif search_type == input('3' ):
print('Check landline phone')
else:
print('Query type input error')
elif choice == input("2"):
print("Check water meter")
elif choice == input("3"):
print ("human service")
else:
print("input error")

Correction:

msg = """
Welcome to call 10086
1. Check the phone bill
2. Check the water meter
3. Manual service

"""
print(msg)

choice = input("Please select the service you need")
if choice == "1":
print("1. Check this machine; 2. Check other people's mobile phones; 3. Check the landline")
search_type = input("Please enter the desired Query type: ")
if search_type == '1':
print('Query this machine')
elif search_type == '2':
print('Query other people's mobile phones')
elif search_type == '3':
print('Query Landline')
else:
print('Query type input error')
elif choice == "2":
print("Check water meter")
elif choice == "3":
print("Human service")
else:
print("Enter mistake")


10. Loop statement

while condition:
the condition is met and executed


while True:
print('Fishing to catch saury, saury to fish on the island')


while 1==1 and 2==2:
print('fishing to catch saury, saury to fish on the island')

timer = 0
while timer < 3:
print('fishing to catch saury, saury to fish on the island')
timer = timer + 1

print('Complete')


Exercise 1: Output 1 - 10 on the page

count = 1
while count < 11:
print(count)
count = count + 1



- break, forcibly terminate the current loop
while True:
print('Fishing to fish with swordfish , the saury is going to fish on the island')
break

exercise 2: output 1 - 10 on the page (using break)
count = 1
while True:
print(count)
count = count + 1
if count == 11:
break

count = 1
while True :
print(count)
if count == 10:
break
count = count + 1

- continue, jump out of this loop and continue to the next loop.



Exercise
1. Print 1 - 10 on the page

count = 1
while count < 11:
print(count)
count = count + 1

count = 1
while True:
print(count)
count = count + 1
if count == 11:
break

count = 1
while True:
print(count)
if count == 10:
break
count = count + 1
2. 页面上输出 1 - 10,排除7
count = 1
while count < 11:
if count == 7:
count = count + 1
continue
print(count)
count = count + 1


count = 1
while count < 11:
if count == 7:
pass
else:
print(count)
count = count + 1

其他练习题:
3、求1-100的所有数的和

sum = 0

count = 1
while count < 101:
sum = sum + count
count = count + 1

4, output all odd numbers from 1-100
n=3
div = n%2

5, output all even numbers from 1-100

6. Find the sum of all numbers from 1-2+3-4+5 ... 99




11. Common data types

Integer:
age = 18
String:
name = "Lagerstroemia"
# Get purple
n1 = name[0]
n2 = name[1]
list:
user_list = ["Lagerstroemia","Erkang","18","Massage","Chicken"]
n3 = user_list[0]
n4 = user_list[1] # "Erkang"

user_list = ["Lagerstroemia","Erkang","18","Massage","Chicken"]

for xxx in user_list:
print(xxx)
if xxx == '18':
break
dictionary:
user_info = {"name":"Lagerstroemia","age":18}


n5 = user_info["name"]
n6 = user_info[" age"]

user_info['count'] = 666
# {"name":"Lagerstroemia","age":18,"count":666}

Data type nesting:

n7 = ["alex","eric",[ 11,22,33]]

n7[1]
n7[2][1]

n8 = [
"alex",
{'name':'日天','age':18},
[11,22,33]
]
n8[1]["age"] = 19




problem: loop through the user list, print user names and passwords
user_list = [
{'username':'alex', 'password':'123', 'count':0 },
{'username':'eric', 'password':'123', 'count':0 },
{'username':'tony', 'password':'123', 'count':0 },
{'username':'oldboy', 'password':'123', 'count':0 },
]

for item in user_list:
print(item['username'],item['password'],item['count'])

问题: 将每个用户的count改成1
user_list = [
{'username':'alex', 'password':'123', 'count':0 },
{'username':'eric', 'password':'123', 'count':0 },
{'username':'tony', 'password':'123', 'count':0 },
{'username':'oldboy', 'password':'123', 'count':0 },
]
for item in user_list:
if item['username'] == 'alex' and item['password'] == '123':{'username ':'eric', 'password':'123'},{'username':'alex', 'password':'123' },user_list user_list = [problem: The user enters the username and password, and checks it inprint(item)
item['count'] = 1






{'username':'tony', 'password':'123'},
{'username':'oldboy', 'password':'123'},
]


user = input("Please enter username:")
pwd = input("Please enter password:")


flag = False

for item in user_list:
if item['username'] == user and item['password'] == pwd:
flag = True
break
else:
pass

if flag:
print ("Login successful")
else:
print("Login failed")


Content sorting:
1. Programming language
- c and others
- interpretation and compilation
- difficulty
2. Interpreter
- cpython

3. Syntax rules
- header
- #! /usr/bin/env python
- # -*- coding:utf-8 -*- (if there is Chinese in the code in py2, you must find a head)
- encoding
- ascii
- unicode
- utf-8
- gbk

PS: one byte = 8 bits 00000000

- input output
- input
- row_input (py2)

- print

- variable
- specification:
- letter, number, underscore
- number cannot start
- cannot be a built-in keyword
suggestion: See the name

- Conditional statement
if Condition:
pass
else:
pass

if Condition:
pass
elif Condition:
pass


- Loop statement:
while Condition:
pass

Keyword:
break
continue


while True:
print(1)
while True:
print(2)
break

Note: Indent

- Data Type
- Integer
age = 19
- String
name = "xxxx"

- list
names = ["xx","xx","xx"]

-dictionary
info = {
"name":'alex', # key-value pair
"age":'18'
}

 

Guess you like

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