Using Python to learn basic one

Source: Internet
Author: User
First, install the PYTHON34

Windows

Download the installation package and install it on the Python website (https://www.python.org/downloads/).

The default installation path for Python is: C:\Python34

Configure environment variables: "Right-click Computer"-"Properties"-"Advanced system Settings"-"Advanced"-"Environment variable"-"in the Second content box find a row of the variable named path, double-click"-"Python installation directory appended to the variable value, with;

Two, the first Python program

1. Execute in the interactive: directly invoke Python's own interactive run code (for temporary debugging)

2. Write the program in the file to execute

(1) Installing Pycharm (http://www.jetbrains.com/pycharm/download/)

(2) Create a new project and Python File

(3) Write code

Print ("Hello world!")

(4) Operation

Iii. Defining variables

Variables are stored and called in the program. Variables are equivalent to a container that stores data in memory. (The difference between memory and hard disk: memory is faster than hard disk, memory is temporary storage, hard disk is permanent storage)

#!/user/bin/env python#-*-coding:utf-8-*-user_name = "Grace" #申明一个字符串变量age =        #申明一个数字变量

Rules for variable definitions:
1. Single quotes, double quotes, and triple quotes are all strings
2. Variables should be meaningful and make people more clear
3. Variable names can only be any combination of letters, numbers, and underscores
4. The first character of a variable name cannot be a number
5. Cannot declare variable name with keyword

Four, character encoding

Bytes: 8 bits constitute 1 "bytes", which is the basic unit of measurement for storage space. 1 bytes can store 1 English letters or half Chinese characters, in other words, 1 Chinese characters occupy 2 bytes of storage space.

1kb=1024b 1mb=1024kb 1GB=1024MB 1TB=1024GB

1. ASCII (American Standard Code for information interchange, US Standards Information Interchange Code) is a Latin alphabet based computer coding system, mainly used to display modern English and other Western European languages,
It can only be represented by a maximum of 8 bits (one byte), that is: 2**8 = 256-1, so the ASCII code can only represent a maximum of 255 symbols.
2. It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode
Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets the unity of each character in each language and
and a unique binary encoding, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), i.e. 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more
3. UTF-8, is the compression and optimization of Unicode encoding, he no longer uses a minimum of 2 bytes, but all the characters and symbols are categorized: the contents of the ASCII code is saved in 1 bytes, the characters in Europe are saved in 2 bytes,
East Asian characters are saved with 3 bytes ...
Python2 a few versions of the default Ascill, you can specify a character set:

#!/usr/bin/env python#-*-coding:utf-8-*-  print "Hello, World"

V. Notes
Single-line gaze: # Annotated content
Multi-line Comment: ' commented content ' (' ' represents a multiline string, multiline string is placed directly in Python and will be ignored by Python)
Six, formatted string
1. Input is a string that is received by default in Python3

2.%s can be passed into a string or integer class,%d can only pass in integer class,%f floating-point number type

3. Three single quotation marks ("") can be used for multiple lines of string, define variables as multiple lines of string, output multi-line string

Name = input ("Input your Name:") age = Int (input ("Input your:")) #convert str to intjob = input ("Input your Job:") Messa Ge= ' Information of user%s:_______________________name:%sage:  %fjob:  %s---------End-----------'% (name , name,age,job) print (message)

Vii. Expressions If ... else

Use a login verification to illustrate:

Rightname = "tt" password = "WPL" userName = input ("Please enter your name:") UserPassword = input ("Please enter your PASSWO RD: ") #Python is a forced indent language that controls dependencies by indenting if userName = = Rightname and UserPassword = = password:    print (" Welcome login ... ") E LSE:    Print ("Your user name or user password is invalid")

Use an age-guessing program description:
Demand:
Guess 8 times when you're guessing the wrong things.
Each error 3 times prompts the user whether to continue, user input Y means continue, enter any other characters to exit the program
The user guesses the right on the end of the program

Age = 22
Count = 0
For I in range (10):
Print ("-->counter", Count)
If Count < 3:
guess_age = Int (input ("Please input:"))
if guess_age = = Age:
Print ("You is Right")
Break
Elif guess_age > Age:
Print ("Think smaller!")
Else
Print ("Think bigger ...")
Count + = 1
Else
User_answer = input ("Do want Countine:")
if User_answer = = "Y":
Count = 0
Else
Print ("Bye")
Break

The code has a maximum of 10 loops, with two loops asking the user to continue guessing, and not having the user guess the age at the time when the user responds with Y.
Instead, start guessing age after entering the next cycle.

Nine, the first knowledge of the module

import sysprint (Sys.path) #打印python的环境变量地址 import the Sys module and invoke the path data in the module. Note: The standard library is generally placed in the <python installation path >\\lib third-party libraries are generally placed in the <python installation path >\\lib\\site-packages (their own Python files are put into that directory, There are several ways that you can import the file and call the method and data when you write other modules: Import Osos.system ("dir") #执行系统命令, print only the command result, not save (directory under current path) Cmd_res = Os.popen (" Dir ") #执行命令并把结果保存到一个文件中print (Cmd_res.read ()) #读取这个文件并打印出结果 Os.mkdir (" Other_dir ") #在当前路径下创建一个新目录 

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.