Python [1]-basic knowledge and basic knowledge of python

Source: Internet
Author: User
Tags cmath

Python [1]-basic knowledge and basic knowledge of python
1. Introduction

Python is an explanatory, object-oriented, advanced programming language with dynamic semantics.

Liao Xuefeng Website: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000

Download python: https://www.python.org/downloads/

Interactive python Interpreter: IDLE

Python organizes code segments by indentation, unlike the braces in c. We recommend that you use four spaces for indentation.

2. Expression

Division:

By default, an integer is divided by another integer, and the result is still an integer: 1/2 = 0

If one of them is a floating point number, the result is a floating point number.

1.0/2 =>0.5

1/2.0=>0.5

1/2. =>0.5

Double diagonal lines: the result is divisible even if the parameter is a floating point.

1.0//2=》0.0

Power Operation:

2**3=>8

-2**3=>-8

Obtain the user input: input (str) method.

>>> x=input('x=')
x=5
>>> y=input('y=')
y=6
>>> print x*y
30

3. Function

Power function: pow (2, 3) => 8

Absolute Value: abs (-10) => 10

Round: round (0.4) => 0.0 round (0.6) => 1.0

4. Module

AvailableImportImport commands into special modules to enhance functions.

For example:

import math

math.floor(1.9)

=>1.0

Cmath and plural:

>>> import cmath
>>> cmath.sqrt(-1)
1j

Python itself supports complex computing:

>>> (1+2j)+(3+4j)
(4+6j)
>>> (1+2j)-(3+4j)
(-2-2j)
>>> (1+2j)*(3+4j)
(-5+10j)

5. Save and execute

Python saves files with the suffix ". py"

6. Note

The # Well number is used to indicate comments. The content on the right of the well number will not be interpreted.

7. String

You can use single or double quotation marks for a string. If the content of the intermediate string also contains quotation marks, you can use escape characters.

>>> "let's say \"hello world\""
'let\'s say "hello world"'

Concatenated stringUse the plus sign:

>>> x='a'
>>> y='b'
>>> x+y
'ab'

String Representation: str and repr

  • Str: converts a value to a reasonable string for your understanding;
  • Repr: converts a value to a reasonable python expression.

>>> x="hello world";
>>> print str(x)
hello world
>>> print repr(x)
'hello world'
>>> y=10000L
>>> print str(y)
10000
>>> print repr(y)
10000L

Input and raw_input

  • Input assumes that the user inputs a valid python expression. In the following example, only the input string is successfully executed.
  • Raw_input uses the input as the original data and puts it into the string.

We recommend that you use raw_input.

# Using Input

>>> Name = input ('your name? ')
Your name? Chenjing
Traceback (most recent call last ):
File "<pyshell #19>", line 1, in <module>
Name = input ('your name? ')
File "<string>", line 1, in <module>
NameError: name 'chenjing 'is not defined
>>> Name = input ('your name? ')
Your name? 'Chenjing'
>>> Print 'hello' + name
Hellochenjing

# Use raw_input

>>> Name = raw_input ('your name? ')
Your name? Chenjing
>>> Print 'hello' + name
Hello chenjing

Note that raw_input is returned as a string. If you want an integer, you need to use int () for type conversion. For example:

Birth = int (raw_input ('ur birth '))
If (birth <2000 ):
Print 'before 00'
Else:
Print 'after 00'

Long String

If a long string needs to span rows, you can use three quotation marks, and the quotation marks do not need to use escape characters.

>>> str='''hello world
hello 'cathy'
hello chenjing'''
>>> print str
hello world
hello 'cathy'
hello chenjing

Original string

The original string starts with r. The original string does not treat the backslash as a special character. Each character entered in the original string is directly output in the same way, but the original string cannot end with a backslash.

c:\program files
>>> print r'c:\newfile'
c:\newfile

String Encoding

ASCII encoding is 1 byte, while Unicode encoding is usually 2 bytes.

UTF-8 encoding: Variable Length Encoding. Encodes a Unicode character into 1-6 bytes based on different numbers. Common English letters are encoded into 1 byte, and Chinese characters are usually 3 bytes, only uncommon characters are encoded into 4-6 bytes.

In computer memory, Unicode encoding is used in a unified manner, when you need to save to the hard disk or need to transfer, it is converted to UTF-8 encoding.

Python supports ASCII encoding. Through the ord () and chr () functions, letters and numbers can be converted to each other.

print ord('A') >>>65
print chr(65) >>>A

Later, Python added support for Unicode.u'...'Indicates

U'test'

To convert to UTF-8, The encode ('utf-8') method is required.

U'test'. encode ('utf-8 ')

8. time and date

The built-in datetime module of Python provides common time types such as datetime, date, and time.

  • The date () and time () methods can extract the date and time respectively;
  • The strftime () method is used to format the time as a string;
  • The strptime () method is used to convert a string to a date format;
  • The time difference of the delta type can be obtained by subtraction of the two time periods.

#-*-Coding: cp936 -*-

From datetime import datetime, date, time

Dt = datetime)

Print (dt. day) #23

Print (dt. hour) #14

Print (dt. date () #2016-05-23

Print (dt. time () #14:40:00

# Format a string

Print (dt. strftime ("% Y-% m-% d % H: % M ") #

# String Conversion to time

Dt = datetime. strptime ('20140901', '% Y % m % d % H % m ')

Print (dt) #14:40:00

# Time Difference

Dt1 = datetime (, 40)

Dt2 = datetime (Panel 5, 1)

Delta = dt1-dt2

Print (type (delta) # <type 'datetime. timedelta '>

Print (delta) #22 days, 14:40:00

Print (dt + delta) #05:20:00

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.