Python Learning Week 1 (Python3.x), pythonpython3.x

Source: Internet
Author: User

Python Learning Week 1 (Python3.x), pythonpython3.x

The first week of learning records, this phase of learning the Python syntax.

1. Develop the IDE using PyCharm. After creating a project, you can set some default code in it.

# Author:BH

Here are the steps for setting PyCharm:

Click --> edictor --> Code Style --> File and Code Templates --> Python Script

Enter the content to be added in the right. (This part has a note, as mentioned later) and then apply --> OK can be set successfully. You need to create a new Python file to see the effect.

For more information about program running, right-click and choose --> Run

Ii. Procedures

2.1 Hello World

print ("Hello World!")

Direct output:

Hello World!

Process finished with exit code 0

2.2 Variables

name = "Hei Han"name2 = nameprint("My name is ",name, name2)name = "Xu Da"print(name,name2)gf_of_zxy = "Liu De Hua"

When defining a variable, you can directly write the variable name. This is a simple definition that is different from the OC/C language in iOS, in OC/C, you must first set the data type. Take OC as an example.

Nsstring *name = @"Hei Han"

In Python, you do not need to specify the data type, which is concise. When printing name, it is also different from OC. OC must be followed by placeholders, such as % @ and % d ....

In Python, long variables are expressed in lower case and underlined characters. In OC, the first word is in lower case and the first letter is in upper case, all developers in a language have their own rules.

2.3 input and condition judgment

# Author: BHimport getpass_username = "bh" _ password = "123" username = input ("username:") # password = getpass. getpass ("password:") # ciphertext input getpass is difficult to use in pycharm password = input ("password:") # print (username, password) if username = _ username and password = _ password: print ("Welcome user {name} login... ". format (name = username) else: print ("Invalid username or password ")

The above code is a simple logon applet. Here we need to learn about the input

Input function, which defines username and password for input. When the input function is used and the program runs, the user is asked to perform input operations.

Condition judgment, if... else, the syntax here is different from OC, and "and" OC is the logical operator "&", and the code in if is not {}, but used: to determine the scope

Note that the following code must be indented in condition judgment. Otherwise, an error is reported. indentation indicates that the scope of the code below belongs to the if statement.

The above Code also contains the import getpass. import indicates the import framework/class library, which means that getpass is a library in Python that displays the ciphertext of the password, but in PyCharm, the support for this library is not good. We can execute this script on the terminal, and we can see that it is ciphertext. The preceding figure shows the formatting output. The following describes several types of formatting output in Python.

2.4 annotations

Python comment, single line comment: Add # code before the code

Comment on multiple lines: '''code '''

''' Can also be used to represent strings of multiple rows.

2.4 string formatting

# Author: BH # raw_input 2.x input 3. xname = input ("name:") age = int (input ("age:") # forcibly convert print (type (age), type (str (age ))) # print data type job = input ("job:") salary = input ("salary:") info = ''' ---------- info of % s --------- Name: % sAge: % dJob: % sSalary: % s ''' % (name, name, age, job, salary) info2 = ''' ------------ info of {_ name} --------- Name: {_ name} Age: {_ age} Job: {_ job} Salary: {_ salary }'''. format (_ name = name, _ job = job, _ age = age, _ salary = salary) info3 = ''' ------------ info of {0} --------- Name: {0} Age: {1} Job: {2} Salary: {3 }'''. format (name, job, age, salary) print (info3)

Three types of strings, formatting


info = '''------------ info of %s ---------Name:%sAge:%dJob:%sSalary:%s''' % (name,name,age,job,salary)

Similar to the OC language, placeholder is used. % s indicates the string, % d indicates the integer, and % f indicates the floating point type. The last % (variable name) is added)


info2 = '''------------ info of {_name} ---------Name:{_name}Age:{_age}Job:{_job}Salary:{_salary}''' .format(_name = name,            _job = job,            _age = age,            _salary = salary)

{Parameter} placeholder, and finally. format () assign values to each parameter.

info3 = '''------------ info of {0} ---------Name:{0}Age:{1}Job:{2}Salary:{3}''' .format(name,job,age,salary

This method {digit order} is followed by. format (variable name). This method is not good. It is difficult to express the specific meaning of numbers such as 0 1 2, which is not recommended.


2.5 cycles

2.5.1 while


# Author:BHcount = 0while True:    print("count:",count)    count += 1    if count == 1000 :        break

Similar to OC while condition: Indentation enters loop Scope


for i in range(10):    print("-------",i)    for j in range(10):        print(j)        if j >5:            break

Format: for temporary variable in range (number of cycles ):

The break and continue keywords are designed in the loop. Similar to the OC language, the break jumps out of the current loop and the loop ends. The continue jumps out of the current loop and continues the next loop.


The red letter mentioned above refers to the Python version. Now, Python2.x and Python3.x are based on Python3.x.

Note 1: Python2.x does not support Chinese characters. Therefore, you must add a code at the top of the script program.

# -*- coding:utf-8 -*-

The code is UTF-8 encoded. Otherwise, an error is returned when you enter Chinese characters. Python3.x does not exist.

Note 2: The input values of raw_input and Python3.x in Python2.x are equivalent.

NOTE 3: In Python3.x, the input function is of the string type by default. If you want to enter an integer, You Need To forcibly convert it.

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: 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.