The first day of Python learning

Source: Internet
Author: User

Under Windows Python uses:

Official website Download: www.python.org

Available in 2.* and 3.* versions

I'm using the Python3 version.

Download the installation package installation-Remember to check the Add PATH environment variable when installing, so you can enter Python in cmd, bring up the Python interactive interface

Press the shortcut key under Windows Win+r mediation Run--input cmd to bring up cmd interface--Input python

This goes into Python's interactive interface as follows:

Microsoft Windows [version 10.0.15063] (c) Microsoft Corporation. All rights reserved. C:\users\weixi>pythonpython 3.6.1 (V3.6.1:69C0DB5, Mar, 17:54:52) [MSC v.1900 + bit (Intel)] on Win32type "Hel P "," copyright "," credits "or" license "for more information.>>>

To exit Python's interactive interface:

Quit ()

C:\users\weixi>pythonpython 3.6.1 (V3.6.1:69C0DB5, Mar, 17:54:52) [MSC v.1900 + bit (Intel)] on Win32type "Hel P "," copyright "," credits "or" license "for more information.>>> quit () c:\users\weixi>


Write the first Python program:

Syntax: print ("Input content") # #打印出来显示到屏幕上

>>> print ("Hello world!") Hello world!>>> print ("Hello python!") Hello python!

This is done in the Python interface, and after the CMD interface is closed, the program disappears because it exists in memory.

It can be written to a file so that it is permanent and executes when needed.


    • Create a new file on the Windows desktop Helloworld.txt content for print ("Hello World")

    • In cmd, go to the location where Helloworld.txt is.

    • Execute Python helloworld.txt

C:\USERS\WEIXI>CD Desktopc:\users\weixi\desktop>dir helloworld.txt The volume in drive C is the serial number of the WINDOWS volume is 7066-1411 C:\Users\               Weixi\desktop's catalogue 2017/08/13 Sunday 22:30 helloworld.txt 1 files 22 bytes 0 Directories 18,693,619,712 Available Bytes C:\users\weixi\desktop>c:\users\weixi\desktop>python Helloworld.txthello world!

(The name of my new file above is helloworld.txt, and the suffix is txt.) Generally, when you create a new Python file to name the file, end with a. py. This makes it easy for the system to identify and we recognize that he is a Python program)


To calculate in Python:

Enter the Python interface

c:\users\weixi\desktop>pythonpython 3.6.1  (v3.6.1:69c0db5, mar 21 2017,  17:54:52)  [MSC v.1900 32 bit  (Intel)] on win32type  "Help",  " Copyright ", " credits " or " license " for more information.>>> 2  + 3     # #加法运算5 >>> 5 - 3      # #减法运算2 >>> 2 * 3     # #乘法运算6 >>> 6 /  2     # #除法运算, the result is a floating point 3.0>>> 6 // 2     # #除法运算, the result is an integer 3>>> 5 // 22>>> 5 % 2      # #取余数1 >>> 4 % 20>>> 14 % 54>>> 2  % 42>>> 2 % 52>>> 6 % 76>>> 6 %  226>>>>>> 2 ** 2   # #次方计算4 >>> 2 ** 38>>> 3 ** 29 

When the remainder is found, the remainder = dividend when the divisor < divisor is taken




Variables in Python:

The variable names defined in Python can be: uppercase and lowercase letters, underscores, combinations of numbers

It is important to note that variable names cannot begin with a number

It is also not possible to conflict with some of the built-in functions and keywords in python, such as the following:

    650) this.width=650; "Src="/e/u261/themes/default/ Images/spacer.gif "alt=" * "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat Center; border:1px solid #ddd; "/> The following keywords cannot be declared as variable names
         [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ',              ' from ',      ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' Try ', ' while ',              ' with ', ' yield ']

C:\users\weixi\desktop>pythonpython 3.6.1 (V3.6.1:69C0DB5, Mar, 17:54:52) [MSC v.1900 + bit (Intel)] on win32t Ype "Help", "copyright", "credits" or "license" for more information.>>> a = 2>>> b = 3>>> C = A + b>>> c5>>> a = 2>>> B = a>>> b2>>> a = 2>>> B = 3>>> A2 >>> B3



Get user input:

Syntax: input ("hint message")--input content

>>> input ("Please input:") input:test ' test ' >>>

Input is the type of consciousness that waits for the user to enter content in Python

You can also define variables to print the input content.

>>> a = input ("Please input:") input:hehehe>>> print (a) hehehe



Lab tasks:

make a simple gadget :

Make an interactive addition calculator

Make an interactive subtraction calculator

Make a calculator that divides evenly

Make a calculator for floating-point arithmetic division

Make a calculator that takes the remainder



Idea: Enter a number to give it to the variable, and then enter a number to give it to the variable, the variable 1 and the variable 2 to operate, the result is given to the variable 3

The final printout of the variable 3 is the result of the operation.

A = input ("Enter first number:")

b = Input ("Enter a second number:")

c = A + b

Print (c)


>>> print (a) hehehe>>> a = input ("Input:") input:3>>> B = input ("Input:") input:2>>> c = A + b>>> print (c) 32

The result is not what I thought, 3 + 2 = 5 Why is it equal to 32?

Originally, what I entered was string type, string 1+ string 2 = string 1 string 2. That's not what I want.

What to do?

You need to use the "int ()" function to convert the string type to a number of integer type.


The program is optimized as follows:

A = input ("Please inpute NUM1:")

b = Input ("Please inpute num2:")

A = Int (a)

b = Int (b)

c = A + b

Print (c)

Write code to the comput.py file

The results of the implementation are as follows:

C:\users\weixi\desktop>python compute.pyplease inpute num1:22please inpute num2:3355

This will calculate the result I want!

A few other operations are the same, that is, the operator is replaced by -*/ //% these can be


A = input ("Please inpute NUM1:")

b = Input ("Please inpute num2:")

A = Int (a)

b = Int (b)

c = a +-*///% B

Print (c)


This article is from the "longing for Technology small white" blog, please make sure to keep this source http://lesliecheung.blog.51cto.com/12622169/1955983

The first day of Python learning

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.