week1 notebook1


First acquaintance with Python

1. Introduction to python

- Interpreter:
cpython (default)
ipython(shell)
jpython(java)
ironpython
rubypython

- encoding:
ascii: use one byte = 8 bits to identify everything that the computer can express
unicode: universal code , use 4 bytes = 32 bits for the corresponding relationship
utf-8: compress for the universal code, use at least 1 byte to represent
gbk: the corresponding relationship for the text of Asian countries
Chinese 2 bytes = 16 bits
Note : Chinese occupies 3 bytes in utf-8 encoding, and Chinese occupies 2 bytes in gbk

- Version:
python2: interpreter default encoding: ascii
format: # -*- coding:utf-8 -*-

python3: interpreter Default encoding: utf-8
format: no need to configure encoding format

- IDE:
- pycharm
- text size
- template
- vim
- notepad

Second, module:
- time:
The time module provides various time-related functions.
Time-related modules include: time, datetime, calendar

- getpass:
encrypted input password

- os:
contains common operating system functions


3. Syntax:

- Input, output:
print:
print(" You are crazy, he is stupid")
input:
# cipher text password
# import getpass
# user = input("enter a user,please")
# passwd = getpass.getpass("enter a num,please")
# print(user ,passwd)

- variable:
Format: variable name = value
Specification:
a. Numbers, letters, underscores
b. Cannot start with numbers
c. Cannot use python keywords
Suggestion: Concise and understand; user_pwd = "xxx"
Example:
name = 'anthony'
user = 'anthony'

- data type:
age = 18 # integer type
name = "anthony" # string type
list:
user_list = ["Lagerstroemia","Erkang","18","Mass" ,"Chicken"]
n3 = user_list[0]
n4 = user_list[1] # "Erkang"

user_list = ["Lagerstroemia","Erkang","18","Mass","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":"紫薇","age":18,"count":666}

Data type nesting:

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

n7[1]
n7[2][1]

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


- Conditional statement:
Format:
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:
After success go here
else:
All of the above fail
Example:

# while True:
# # Define category
# msg = '''
# Welcome to the big world
# 1, Coke
# 2, Sprite
# 3, Lemon Tea
# 4, Exit
# '''
# # print Category
# print(msg)
# # Define options
# choice = input("Please enter the category you choose:")
#
# # Judge the selected category
# if choice == '1':
# print("1, Vodka\n2, wine\n3, liquor 4, exit")
# # Define wine category
# search_type = input("Please enter your selected wine category:")
# if search_type == '1':
# print ("Vodka")
# elif search_type == '2':
# print("Wine")
# elif search_type == '3':
# print("baijiu")
# else:
# print("input error")
# break
# elif choice == '2':
# print("Sprite")
# elif choice == '3':
# print("lemon茶")
# elif choice == '4':
# break
# else:
# print("Input error")

- Loop statement:
Format:
while Condition: The
condition is met and execute

while True:
print('Fishing is to fish for saury, and for saury Go to the island to fish')

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 go fishing on the island)
timer = timer + 1

print ('完成')

Example:
# count = 1
# while count < 11:
# if count == 7:
# count +=1
# continue
# print(count)
# count +=1

- Loop exit:
- break:
Definition: Forcibly terminate the current loop
Example:
Example 1:
while True:
print('fishing to catch saury, saury to fish on the island')
break
Example 2:
output 1 - 10 on the page

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

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

c. count = 1
while True:
print(count)
if count == 10:
break
count = count + 1
- continue :
Definition: Jump out of this cycle and continue to the next cycle.
Example:
Example 1:
a. count = 1
while count < 11:
if count == 7:
count = count + 1
continue
print(count)
count = count + 1


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

- looping exercise:
a. 1 Sum of all numbers of -100
# define base
sum = 0
count = 1

# loop start
while True:
if count == 100:
break
else:
count += 1
sum += count
print(sum)

b. 1-2+3-4+5 ... 99的所有数的和
sum = 0
for count in range(1,101):
if count % 2 == 1:
sum -= count
elif count % 2 == 0:
sum += count
print(sum)



Guess you like

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