python2.5/2.6 Practical Tutorial Basics _python

Source: Internet
Author: User
Tags exception handling in python
Start walking
Copy Code code as follows:

#! /usr/bin/python

a=2
B=3
c= "Test"
C=a+b
print "Execution result:%i"%c

Knowledge points

Python is a dynamic language, and variables need not be declared in advance.
Print Statements in C style
Strings and Numbers
But interestingly, in JavaScript we would ideally connect strings to numbers, because it's a dynamic language. But it's a little weird in Python, as follows:
Copy Code code as follows:

#! /usr/bin/python

a=2
b= "Test"
C=a+b


Running this line of programs will make an error, prompting you that strings and numbers can't be connected, so you have to use built-in functions to convert
Copy Code code as follows:

#! /usr/bin/python

a=2
b= "Test"
C=str (a) +b
d= "1111"
E=a+int (d)
#How to print multiply values
Print "C is%s,e is%i"% (c,e)

Knowledge Points:

converting strings and numbers with int and STR functions
Printing starts with a #, not a habit//
How to print multiple parameters
Internationalization
Tired of writing English notes, we want to use Chinese!


#! /usr/bin/python
#-*-Coding:utf8-*-

Print "God is back in the world: Maradona is the Argentine national football coach."
Knowledge Points:

Add character set to use Chinese
List
List of JavaScript-like arrays for ease of use
Copy Code code as follows:


#! /usr/bin/python
#-*-Coding:utf8-*-

#定义元组
Word=[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G '

#如何通过索引访问元组里的元素
A=WORD[2]
Print "A is:" +a
B=word[1:3]
Print "B is:"
Print B # index 1 and 2 elements of Word.
C=word[:2]
Print "C is:"
Print C # index 0 and 1 elements of Word.
D=word[0:]
Print "D is:"
Print D # all elements of Word.

#元组可以合并
E=word[:2]+word[2:]
Print "E is:"
Print e # all elements of Word.
F=WORD[-1]
Print "F is:"
Print F # The last elements of Word.
G=word[-4:-2]
Print "G is:"
Print G # 3 and 4 elements of Word.
H=word[-2:]
Print "H is:"
Print H # The last two elements.
I=word[:-2]
Print "I is:"
Print I # Everything except the last two characters
L=len (Word)
Print "Length of Word is:" + str (l)
print "Adds new element"
Word.append (' h ')
Print Word

#删除元素
Del Word[0]
Print Word
Del Word[1:3]
Print Word

Knowledge Points:

The list length is dynamic and you can add any deletion element.
Indexing makes it easy to access elements and even returns a child list
For more information, refer to the Python documentation
Dictionary
Copy Code code as follows:

#! /usr/bin/python

X={' a ': ' AAA ', ' B ': ' BBB ', ' C ': 12}
Print x[' a ']
Print x[' B ']
Print x[' C ']

For key in x:
print ' Key is%s ' and value is%s, (Key,x[key])

Keys=x.items ();
Print Keys[0]
keys[0]= ' DDD '
Print Keys[0]

Knowledge Points:

Use it as a map of Java.
String
It's so much more impressive than the way C/c++,python handles strings. Use a string as a list.

Copy Code code as follows:

Word= "ABCDEFG"
A=WORD[2]
Print "A is:" +a
B=word[1:3]
Print "B is:" +b # 1 and 2 elements of Word.
C=word[:2]
Print "C is:" +c # 0 and 1 elements of Word.
D=word[0:]
print ' d is: ' +d # all elements of Word.
E=word[:2]+word[2:]
print ' E is: ' +e # all elements of Word.
F=WORD[-1]
print ' F is: ' +f # The last elements of Word.
G=word[-4:-2]
Print "G is:" +g # 3 and 4 elements of Word.
H=word[-2:]
print ' H is: ' +h # The last two elements.
I=word[:-2]
print ' I is: ' +i # Everything except the last two characters
L=len (Word)
Print "Length of Word is:" + str (l)

However, note the difference between ASC and Unicode strings:
Copy Code code as follows:

#! /usr/bin/python
#-*-Coding:utf8-*-

S=raw_input ("Enter your Chinese name, press ENTER to continue");
Print "Your name is:" +s;

L=len (s)
Print "The length of your Chinese name is:" +str (L);
A=unicode (S, "UTF8")
L=len (a)
Print "Sorry, I just calculated the error. We should use UTF8 to compute the length of the Chinese string, \
The length of your name should be: "+str" (l);

Knowledge Points:

transcoding with a Unicode function
Conditions and looping statements
Copy Code code as follows:

#! /usr/bin/python
X=int (Raw_input ("Please enter an integer:"))
If x<0:
X=0
Print "Negative changed to zero"

Elif x==0:
Print "Zero"

Else
Print "More"


# Loops List
A = [' Cat ', ' window ', ' defenestrate ']
For X in a:
Print x, Len (x)

Knowledge Points:

Conditions and looping statements
How to get the console input
Function
Copy Code code as follows:

#! /usr/bin/python
#-*-Coding:utf8-*-

def sum (a,b):
Return a+b


Func = Sum
R = Func (5,6)
Print R

# Provide default values
def add (a,b=2):
Return a+b
R=add (1)
Print R
R=add (1,5)
Print R

An easy to use function
Copy Code code as follows:

#! /usr/bin/python
#-*-Coding:utf8-*-

# the range () function
A =range (5,10)
Print a
A = range ( -2,-7)
Print a
A = range ( -7,-2)
Print a
A = Range ( -2,-11,-3) # The 3rd parameter stands for step
Print a

Knowledge Points:

Python does not use {} to control the structure, he forces you to write the program with indentation, so that the code is clear.
Defining functions is convenient and simple
Convenient and easy to use range function
Exception handling
Copy Code code as follows:

#!/usr/bin/python
s=raw_input ("Input your Age:")
if s = = "":
raise Exception ("Input must no be empty.")

Try:
I=int (s)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unknown exception!"
Else: # It is useful for code so must be executed if the ' try clause does not raise a exception
print ' You are %d '% i, ' years old '
finally: # Clean up action
print ' goodbye! '

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.