Python Learning Path (first week)

Source: Internet
Author: User
Tags naming convention

has been on the road to software development for three years. I am an Android native developer born. During the period due to the needs of the work and development trend, also embarked on the road of mixed development, now mainly use the Ionic framework for mobile app development. But the future trend of Internet development is big Data + artificial intelligence. So it's necessary to learn Python now. Here does not introduce the advantages and disadvantages of Python language, want to specifically understand the small partners can self-degree Niang bar.

Okay, go straight to the chase.

This blog mainly record their next few months of the Python learning path, limited capacity, during the period of what is wrong to welcome everyone message area criticism!

Python version: python3.5+

Development tools:Pycharm,:https://www.jetbrains.com/pycharm/

Alternate tool:Anaconda,:https://www.anaconda.com/download/

A lot of Python-used packages are installed in the Anaconda. Very convenient for later development and use. Of course, for beginners to directly install python3.5+ on the line.

Pycharm is a very good Python development tool, a lot of ways to crack online. Please get it yourself.

First, the development of our initial Python program, "Hello world!"

After you have installed Python, open a command-line window, enter a python carriage return, and see the following information, which formally enters the Python environment. You can enter code directly in this window to execute a python program.

At this point enter:

Print ("Hello world!")

The carriage return will be output

Hello world!.

You can also execute the. py file on your hard disk in the command window.

We open Notepad, enter: Print ("Hello world!"), save the file with the suffix named. py.

CD to the current. py file directory.

Execute the command to run the. py file.

This is a simple operation of the command window. Behind the study we mainly carried out in the pycharm.

Second, create our first Python program.

Open Pycharm and create the first Python program. File--new Project

    • Variable

The naming convention for variables is not mentioned here, but it is only necessary to point out:

Python does not need to specify a type to create a variable. For example, we create a python variable by simply:

Name= "Dingshuangdian"

Age=18

Print (' My name is ', name, ' My age ', age ')

 

Print continuously outputs multiple strings separated by ",".

    • User inputs (input)

Name=input ("Name:")
Age=input ("Age:")
Job=input ("Job:")
Salary=input ("Salary:")

Format Stitching output:

1, the first way

Info= ""
---------------Info of%s---------------
name:%s
age:%s
job:%s
salary:%s
"% (name,name,age,job,salary)
Print (info)

Note herethat% (name,name,age,job,salary) must correspond to the defined format one by one,

%s represents a string type. If age is defined as%d, it means that age can accept only integer types.

At this point, Age=input ("Age:") (the default input is a string, which can be displayed through print (type ()) requires a first conversion to an integral type.

That is: age=int (Input ("Age:"))

2, the second way

Info2= ""
---------------info of {_name}---------------
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
". Format (_name=name,_age=age,_job=job,_salary=salary)
Print (Info2)

 3. The Third Way
Info3= ""
---------------info of {0}---------------
NAME:{0}
Age:{1}
JOB:{2}
SALARY:{3}
". Format (name,age,job,salary)
Print (INFO3)

The three formatting outputs are the same, and the second one is recommended.

Input Password Cipher Entry method: Import the official package Getpass

Import Getpass
Password=getpass.getpass ("Password:")

Save as a. py file, run with a command, and you can see the input as hidden.

    • If Else process judgment

Example: We first define a variable that executes different output statements by entering different values from the user.

 apple=25 
Guess_apple=int (Input ("Apple:"))

If guess_apple==apple:
Print ("Congratulations, you guessed it!")
elif guess_apple>apple:
Print ("Not so much, go to the small guess ~")

Else:
Print ("Get close and guess ~")

This is a simple if else judgment statement. It should be noted that Python differs from other languages such as JAVA,JS without the need for brackets to include execution statements. Python performs a strict indentation. The same indentation belongs to the sibling code.
This must be noted.



Next we will change the code, let the user guess 3 times, if you do not guess, quit the program.

while loop.
 apple=25 
count=0 #定义一个计数变量
While count<3: #while条件判断 when Count<3 executes a conditional statement
Guess_apple=int (Input (" Apple: ")
if Guess_apple==apple:
Print (" Congratulations, you guessed it! ") ")
break; #如果猜中, jump out of the loop.
elif guess_apple>apple:
Print ("Not so much, go to the small guess ~")

Else:
If count<2:
Print ("Get close, then guess ~")
Count+=1 #每次执行完条件count加1

Else:
Print ("You've guessed it three times, the game is over!")


Let's change it again, if the user guesses incorrectly three times, instead of letting the program quit, ask the user to continue guessing.

Apple=25
Count=1 #定义一个计数变量
While count<=3: #while条件判断 when Count<3 executes a conditional statement
Guess_apple=int (Input ("Apple:"))
If Guess_apple==apple:
Print ("Congratulations, you guessed it!") ")
Break #如果猜中, jump out of the loop.
Elif Guess_apple>apple:
Print ("Not so much, go for a little bit of guessing ~")
Else
Print ("Get close, Guess again ~")
Count+=1 #每次执行完条件count加1

If count>3:
Y_n=input ("You have guessed the wrong three times, have you continued?") ")
If y_n!= "Y":
Print ("Game Over!") ")
Else
count=1# Initializing a Count variable


2, for Loop.
Apple=25
For I in range (3):
Guess_apple=int (Input ("Apple:"))
If Guess_apple==apple:
Print ("Congratulations, you guessed it!") ")
Break #如果猜中, jump out of the loop.
Elif Guess_apple>apple:
Print ("Not so much, go for a little bit of guessing ~")
Else
Print ("Get close, Guess again ~")



Output even-numbered:
For I in Range (0,10,2):
Print ("Loop", i)

Range The third parameter represents a few outputs. For more information, please refer to the official documentation.

This week the Python introductory course knowledge point is here. Here are some of their own written exercises, you can refer to.

Exercise 1: Writing a login interface
    • Enter User name password
    • Show welcome message after successful authentication
    • The wrong password is locked after three times
Import JSON
Count=1
Username=input ("Please enter user name:")

With open ("User.txt") as F:
UserObject = Json.load (f)
For user in UserObject:
if userName = = user["UserName"]:
UserPassword = input ("Please enter password:")
While Count <= 3:
if UserPassword = = user["UserPassword"]:
Print ("Login successful!")
Count=5
Else
UserPassword = input ("Password is wrong, please re-enter:")
Count + = 1
Else
if Count = = 4:
Print ("Three consecutive password errors, the account has been locked!") ")
Else
Print ("account does not exist!") ")

User.txt Customizing JSON content

Code snippets, the main practice is to judge the use of statements, local files read.
Here, the account password is stored in JSON format in the text local file. You only need to put the password of the input account and the comparison of the file to judge.


Exercise 2: Multilevel menus
    • Three-Level Linkage menu
    • You can choose to go to each submenu in turn

# Create by Dingshuangdian


#省市区三级联动练习


Import JSON
Flagprovice=true
Flagcity=true
Flagarea=true
Countprovice=1
Countcity=1
Countarea=1
Provicelist=[]
Citylist=[]
Arealist=[]
With open ("Province.txt", encoding= ' UTF-8 ') as F:
Areamsg=json.load (f)
For Provice in areamsg:
Provicelist.append (provice["region"])
Print (provicelist)
Selectprovice=input ("Please select Provinces and cities:")
While Flagprovice:
For Sprovice in areamsg:
if Selectprovice = = sprovice["Region"]:
For city in sprovice["Regionentitys"]:
Citylist.append (city["region"])
Print (CityList)
Selectcity=input ("Please select City:")
While flagcity:
For scity in sprovice["Regionentitys"]:
If selectcity==scity["Region"]:
For Selectarea in scity["Regionentitys"]:
Arealist.append (selectarea["region"])
Print (arealist)
Selectarea = input ("Please select City:")
While Flagarea:
For Sarea in Arealist:
If Selectarea==sarea:
Print ("You have selected:", Selectprovice,selectcity,selectarea)
Flagarea=false
Break
Countarea+=1
if (Countarea > Len (arealist)):
Selectarea = input ("The city input is wrong, please re-enter:")
Countarea = 1
Flagcity = False
Break
Countcity+=1
if (Countcity > Len (citylist)):
Selectcity = input ("City input is wrong, please re-enter:")
countcity = 1
Flagprovice = False
Break
Countprovice+=1
if (Countprovice>len (provicelist)):
Selectprovice = input ("The province input is wrong, please re-enter:")
Countprovice=1

This exercise is more difficult for beginners. Main knowledge points list, dictionary, JSON data parsing. You can copy the run reference.

Attached: Provice.txt, provincial and district JSON file:
Baidu Network disk: Https://pan.baidu.com/s/1JtObu2A40QoXDwxJofIsQA



Well, this week's study here is over, there is no shortage of places to welcome comment area advice!













Python Learning Path (first week)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.