Use of Python learning basics One

Source: Internet
Author: User
This article mainly introduces the usage of Python learning basics One. if you are interested, refer to section 1. install Python34.

Windows

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

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

Configure environment variables: right-click the computer and choose Properties> Advanced System Settings> advanced environment variables> Path in the second content box. one row, double-click] --> [append the Python installation directory to the variable value and use; separate]

II. the first Python program

1. execute in the notebook: directly call the python notebook to run the code (for temporary debugging)

2. write the program in a file for execution

(1) install PyCharm (http://www.jetbrains.com/pycharm/download)

(2) create a project and Python File

(3) write code

Print("Hello World!")

(4) run

3. Define variables

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

#! /User/bin/env python #-*-coding: UTF-8-*-user_name = "Grace" # declare a string variable age = 21 # declare a numeric variable

Rule for variable definition:
1. single quotation marks, double quotation marks, and three quotation marks are strings
2. variables should be practical and clear
3. variable names can only be any combination of letters, numbers, and underscores
4. the first character of the variable name cannot be a number.
5. You cannot use keywords to declare variable names.

IV. Character encoding

Byte: eight binary digits constitute one "Byte", which is the basic unit of measurement for the storage space. One byte can store 1 English letter or half a Chinese character. In other words, one Chinese character occupies 2 bytes of storage space.

1KB = 1024B 1 MB = 1024KB 1 GB = 1024 MB 1 TB = 1024 GB

1. ASCII (American Standard Code for Information Interchange) is a computer coding system based on Latin letters. it is mainly used to display modern English and other Western European languages,
It can only be expressed in 8 bits (one byte), that is, 2 ** 8 = 256-1. Therefore, the ASCII code can only represent 255 symbols at most.
2. Obviously, ASCII codes cannot represent all types of characters and symbols in the world. Therefore, you need to create a new encoding that can represent all characters and symbols, that is, Unicode.
Unicode (unified code, universal code, single code) is a character encoding used on a computer. Unicode is generated to address the limitations of the traditional character encoding scheme. it sets a uniform and
The unique binary code indicates that, although some characters and symbols are represented by at least 16 bits (2 bytes), that is, 2 ** 16 = 65536,
Note: Here we will talk about at least 2 bytes, maybe more
3. UTF-8 is the compression and optimization of Unicode encoding, which no longer uses at least 2 bytes, but classifies all characters and symbols: the content in the ascii code is saved in 1 byte, and the European characters are saved in 2 bytes,
East Asian characters are saved in 3 bytes...
The default version of Python2 is ascill. you can specify a character set:

#! /Usr/bin/env python #-*-coding: UTF-8-*-print "Hello, world"

5. Notes
Single row gaze: # commented content
Multi-line comment: ''' commented content ''' (''' indicates a multi-line string, which is directly stored in Python and ignored by Python)
6. format strings
1. in Python3, input receives strings by default.

2.% s can be a string or an integer, % d can be an integer, and % f can be a floating point.

3. three single quotes (''') can be used for multi-line strings. variables are defined as multi-line strings and multiple-line strings are output.

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

7. expression if... else

Use one login verification to describe:

RightName = "tt" password = "wpl" userName = input ("Please enter your name:") userPassword = input ("Please enter your password :") # Python is a forced indent language that controls the subordination through indentation. if userName = rightName and userPassword = password: print ("Welcome login... ") else: print (" your user name or user password is invalid ")

Explain the following with a guess of age program:
Requirements:
You can guess a total of 8 times if you keep making mistakes.
The system prompts the user whether to continue three times. the user enters Y to continue and enters any other characters to exit the program.
The user guessed it and ended the program.

Age = 22
Count = 0
For I in range (10 ):
Print ("--> counter", count)
If count <3:
Guess_age = int (input ("Please input age :"))
If guess_age = age:
Print ("You are right ")
Break
Elif guess_age> age:
Print ("Think smaller! ")
Else:
Print ("Think bigger ...")
Count + = 1
Else:
User_answer = input ("Do you want countine :")
If user_answer = "Y ":
Count = 0
Else:
Print ("bye ")
Break

This code contains a maximum of 10 cycles. two cycles are used to ask whether the user continues to guess. when the user answers "Y", the user does not guess the age in this cycle.
It starts to guess the age only after entering the next cycle.

9. initial knowledge of the module

Import sysprint (sys. path) # print the python environment variable address and import it to the sys module. call the path data in this module. Note: standard databases are generally stored in
 
  
\ Lib third-party libraries are generally placed
  
   
\ Lib \ site-packages (put your own python file in this directory, you can import this file when writing other modules, and call methods and data) several methods of the OS module: import osos. system ("dir") # execute the system Command. only the command results are printed and will not be saved (the directory in the current path) into _res = OS. popen ("dir") # execute the command and save the result to a file print (pai_res.read () # read the file and print the result OS. mkdir ("other_dir") # Create a new directory in the current path
  
 

The above is the usage details of Python learning basics One. For more information, see other related articles in the first PHP community!

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.