Python Learning Notes Day 1
Writing of hello world
print("hello world")
Run the running permission statement:
chmod 755 "C:\Users\liang190327\Desktop\hello world.py"
Among them, 7 is user, 5 is subgroup, and the other 5 is subgroup (sometimes not used, preferably added)
When you make a running package, describe the environment; add:
#!\usr\bin\env python
Hello world in other languages
C++
#include<iostream> int main (void){ std::cout<<"Hello world"; }
C
#include<stdio.h> int main (void){ print ("Helloćworld"); return 0; }
JAVA
public class helloworld{ public class void main(string args[]){ //Output to console System.out.println("hello world "); } }
php
<?php echo "hello world " ?>
ruby
puts "hello world"
go
package main import "fmt" func main(){ fmt.Printf("hello world\n God Bless You");
variable
Naming variables
Similarly different from C, this is omitted
ASCII Code
Common ASCII code size rules: 09<AZ<a~z.
1) Numbers are smaller than letters.Such as "7"< "F";
2) The number 0 is smaller than the number 9 and increases in order from 0 to 9.Such as "3"< "8";
3) The letter A is smaller than the letter Z and increases in order A to Z.Such as "A"< "Z";
4) Capital letters of the same letter are 32 less than lowercase letters.Such as "A"< "a".
Several common letter A SCII code sizes:'A'is 65;'a' is 97;'0'is 48
Go to Baidu ASCII code to see details
Binary
Calculate using Python (2^8):
character
Make and 2.0 compilers prefix because 2.0 does not support Chinese
# -*- coding:utf-8 -*-
User Interaction Mode
Notes
Current line comment: #Comment content, //Comment content
Multi-line comment: """Comment Content"", ##Comment Content#,'''Comment Content''"
User typing (keyboard entry)
Use of input
username=input("username:") password=input("password:") print(username,password)
The blue image below needs to be typed manually;
Formatted output and connector:
Connector: three English commas
Format: info
The overall format is as follows
Form One
info=''' -----info of %s------##%s is the variable above; S is the character meaning, age can also use%d Name:%s #Name: Just text output, any change, can be changed to Chinese, name: the same below Age:%s Job"%s Salary:%s '''%(name,name,age,job,salary) print(info) ```python name=input("name:") age=input("age:") job=input("job:") salary=input("salary:") info=''' -----info of %s------ Name:%s Age:%s Job"%s Salary:%s '''%(name,name,age,job,salary) print(info)
Run as code as shown in Figure:
Form Two
info2=''' -----info of {_name}------##{_name} is the variable above Name:{_name} #_name is used to distinguish the variable named above Age:{_age} Job:{_job} Salary:{_salary} '''.format(_name=name, _age=age, _job=job, _salary=salary) #This refers to python's format function print(info2)
Form Three
info3=''' -----info of {0}------##{0} represents the name variable above Name:{0} Age:{1} Job:{2} Salary:{3} '''.format(name, age, job, salary) #This refers to python's format function print(info3)
The operation diagram is as follows:
if elif else
import getpass _username="liang" _password="12345asdf"; username=input("username:"); password=getpass.getpass("password:"); if _username==username and _password==password: print("Welcome,{name}".format(name=username)); else : print ("ERROR Incorrect username or password") #Note formatting, Python emphasizes formatting, C++ doesn't need it to have parentheses
getpaass module details query
In the diagram below:
while (break)
import getpass _username="liang" _password="12345asdf"; count=0 while count<3: username=input("username:"); password=getpass.getpass("password:"); if _username==username and _password==password: print("Welcome,{name}".format(name=username)); break; else : print ("ERROR Incorrect username or password") if count==2: print ("Attempts have been made three times") count+=1;
The operation diagram is as follows:
for (continue)
for array use
for i in range (10): print ("loop",i) #I is followed by 1, the value is added by one, not added by default, -1 means minus, plus minus
The operation diagram is as follows:
for i in range (2,10,3): print ("loop",i) #Arrays from 2-10, interval 3
The operation diagram is as follows:
Use of continue
for i in range (2,10): if i==7: continue; print (i,"Executed this time"); #Continue is to jump out of this cycle, the cycle below the continue sentence will not execute this time, continue to the next cycle, if-print above is cycle body N.
The operation diagram is as follows:
Jobs:
Note:
Job 2: Use library file calls, build your own user account and password library files, lock the password and use a library file to make calls.
Job 3: You can choose a three-level relationship, such as province-city-county or master-father-child. You can choose one-level relationship, and you can go back to level 2 or 3 and directly to level 1 (province or master).
Job Answer:
Job 2:
The job code is as follows:
#Create a mima.py file in the system library path and reference it in another python file.Code as above #View the current path or system library path method: #The software first looks in the current path, then in the system library #import sys #print (sys.path)#Print system library (that is, environment variables) path #print(sys.argv)#Find the path to print the current file, that is, the absolute path import mima#Introduce
The job code run diagram is as follows
Find the local system libraries (environment variables) on the following path
1 is the system library path
2 Edit file path for current
Assignment 3: (This is a learned or pre-school assignment)
This job is a postcode check for provincial, municipal and county level menus in Guangdong Province
SSX={ "Guangdong": { "Chaozhou":{ "Chaoan" :"445103", "Raoping" :"445122", "Hunan Bridge" :"445102" }, "Dongguan":"523000", "Foshan":{ "Zen City" :"440604", "Golden" :"440608", "South China Sea" :"440605", "Sanshui" :"440607", "Shunde" :"440606" }, "Guangzhou":{ "White clouds" :"440111", "Compliance" :"440184", "Panyu" :"440113", "Beads" :"440105", "a place famous for its flowers" :"440114", "Huangpu" :"440112", "Liwan" :"440103", "Luogang" :"440116", "Nansha Islands" :"440115", "Tianhe" :"440106", "Yuexiu" :"440104", "Zengcheng" :"440183" }, "River source":{ "Dongyuan" :"441625", "peace" :"441624", "Lianping" :"441623", "Longchuan" :"441622", "Source City" :"441602", "Purple gold" :"441621" }, "Huizhou":{ "Purple gold" :"441621", "Boro" :"441322", "Huicheng" :"441302", "Huidong" :"441323", "Huiyang" :"441303", "Dragon Gate" :"441324" }, "Jiangmen":{ "Enping" :"440785", "Heshan" :"440784", "Rivers and seas" :"440704", "Kaiping" :"440783", "Pengjiang" :"440703", "Taishan" :"440781", "New Meeting" :"440705" }, "Jieyang":{ "Welcome" :"445224", "Jiedong" :"445203", "Jiexi" :"445222", "Puning" :"445281", "Rongcheng" :"445202" }, "Maoming":{ "Whitening" :"440923", "Gaozhou" :"440981", "Huazhou" :"440982", "Maogang" :"440903", "Maonan" :"440902", "Xinyi" :"440983" }, "Meizhou":{ "Tai Po" :"441422", "Fengshun" :"441423", "Jiaoling" :"441427", "Meijiang" :"441402", "Meixian" :"441403", "Far" :"441426", "Wuhua" :"441424", "Xingning" :"441481" }, "Qingyuan":{ "Fogon" :"441821", "Liannan" :"441826", "Lianshan" :"441825", "Lianzhou" :"441882", "Qingcheng" :"441802", "fresh" :"441803", "Yangshan" :"441823", "English and German" :"441881" }, "Shantou":{ "Chaonan" :"440514", "Chaoyang" :"440513", "Chenghai" :"440515", "hou kong" :"440512", "Jinping" :"440511", "dragon lake" :"440507", "South Australia" :"440523", }, "Shanwei":{ "City Proper" :"441502", "Hai Feng" :"441521", "Lufeng" :"441581", "Land River" :"441523" }, "Shaoguan":{ "Lechang" :"440281", "Nanxiong" :"440282", "Qujiang" :"440205", "Beneficialization" :"440224", "Milk source" :"440232", "Beginning" :"440222", "Wengyuan" :"440229", "Wujiang" :"440203", "Xin Feng" :"440233", "Shujiang" :"440204" }, "Shenzhen":{ "Bao'an" :"440306", "Fukuda" :"440304", "Longgang" :"440307", "Lo Hu" :"440303", "Nanshan" :"440305", "Salt Field" :"440308" }, "Yangjiang River":{ "Jiangcheng" :"441702", "Yangchun" :"441781", "Yangdong" :"441723", "Yangxi" :"441721" }, "Cloud Float":{ "Rodin" :"445381", "rising" :"445321", "Yunan" :"445322", "Yun'an" :"445323", "Cloud City" :"445302" }, "Zhanjiang":{ "Chikan" :"440802", "Leizhou" :"440882", "Lianjiang" :"440881", "Mazhang" :"440811", "Slope Head" :"440804", "Suixi" :"440823", "Wu Chuan" :"440883", "Xia Shan" :"440803", "Xu Wen" :"440825" }, "Zhaoqing":{ "Deqing" :"441226", "emperor's death" :"441203", "Duanzhou" :"441202", "Seal" :"441225", "Important" :"441283", "quang ninh" :"441223", "Huaiji" :"441224", "Fourth Session" :"441284" }, "Zhongshan":"442000", "Zhuhai":{ "Dome" :"440403", "Golden Bay" :"440404", "Xiangzhou" :"440402" } } } #print(SSX) SR1=input("Please enter a province name:") SR2=input("Please enter the city name:") SR3=input("Please enter the county name, if there is no corresponding level, please enter q: ") if SR3=="q": print(SR1,SR2,"Zip code is:",SSX[SR1][SR2]) else: print(SR1,SR2,SR3,"Zip code is:",SSX[SR1][SR2][SR3])
Run as follows:
Generic use of for
Exception Description
Here we refer to the original article by the CSDN blogger Sinchb. View the original text
SyntaxError: invalid syntax: Code with Chinese, etc. cannot use characters.
NameError tried to access an undeclared variable
ZeroDivisionError divided by 0
SyntaxError Syntax Error
IndexError index out of sequence range
KeyError requests a dictionary keyword that does not exist
IOError Input and Output Errors (e.g. the file you want to read does not exist)
AttributeError attempts to access unknown object properties
ValueError passed an incorrect parameter type to a function, such as passing a string to an int() function