Some basic python syntax

Source: Internet
Author: User
  • String truncation:
print 'kkkjjjsss'[6:8]

 

  • ASCII Code Conversion
print chr(65)

print ord('@')

 

  • Display time:
import datetime

print datetime.datetime.now()

 

  • Sleeping: (seconds)
import time

time.sleep (2.5)

 

  • Calculate the array size
a = [33,44,444]

print len(a)

 

  • Determines whether the array is empty.
a = [33,44,444]

print not a

If false is returned, it is not null, and true is returned, it is null.

 

  • Add something to the end of the array or the member at the end of the pop-up window (if an array is added to the array, it is different from extend, and the entire array is put at the end of the array by an element of course)

 

a = [33,44,444]

a.append('c')

print a

a.pop()

print a

 

 

 

  • Insert c into the 3rd Members of the array, delete the 3rd Members, and set the sequence to 0.
a = [33,44,444]

a.insert(2,'c')

print a

a.pop(2)

print a

 

  • Append an array to the end of the array (if an array is added to an array, different from append, the array is split into members and added to the end of the array one by one)
a = [33,44,444]

r = a + ['c']

print r

r.extend(['ruby','watir'])

print r

 

  • Traversal:
a = [33,44,444]

for i in a:
print i

Note: for in is followed by a colon, indentation, and wordy.

 

  • Retrieve the hash key value and value
a = [33,44,444]

for i,s in enumerate(a):
print "index:" + str(i)
print "value:" + str(s)

Note: Non-string addition must be forcibly converted to string first, OK

 

 

  • Array deduplication
a = [33,44,444,44]

a = list(set(a))

print a

 

Check whether the array contains certain elements

a = [33,44,444,44]

print 33 in a

If you want linux to recognize that the source code is python, add this sentence in the first line of the file,

#! /usr/bin/env python

If you do not want to use python xxx. py to execute your source code, you can assign the source File Execution attributes.

chmod +x myscript.py

Run./xxx. py again.

If you want to write Chinese characters in your python source file, add this sentence in the first line of the file. If you want to add #! /Usr/bin/env python. It's okay to add it to the second line.

# -*- coding: UTF-8 -*-

 

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.