background preloader

Python Basics

Facebook Twitter

Strip whitespace from a list of strings. Python read in string from file and split it into values. Arrays in Python. Dimension?

Arrays in Python

So far everything is going according to plan. Where things start to go wrong just a little is when you attempt to push the similarities between lists and arrays one step too far. For example, suppose you want to create an array initialized to a particular value. Following the general array idiom in most languages you might write: myList=[]for i in range(10): myList[i]=1 only to discover that this doesn't work because you can't assign to a list element that doesn't already exist. One solution is to use the append method to add elements one by one: myList=[]for i in range(10): myList.append(1) This works but it only works if you need to build up the list in this particular order - which most of the time you do.

When you create an array in other languages you can usually perform operations like: Python [sys] 01 Basic Information. Add to a dictionary in Python. Python Loops Cheat Sheet. 8.18. pprint — Data pretty printer — Python v2.7.5 documentation. Source code: Lib/pprint.py The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.

8.18. pprint — Data pretty printer — Python v2.7.5 documentation

If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable. This may be the case if objects such as files, sockets, classes, or instances are included, as well as many other built-in objects which are not representable as Python constants. The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width.

Construct PrettyPrinter objects explicitly if you need to adjust the width constraint. Changed in version 2.5: Dictionaries are sorted by key before the display is computed; before 2.5, a dictionary was sorted only if its display required more than one line, although that wasn’t documented. The os.path module. This module contains functions that deal with long filenames (path names) in various ways.

The os.path module

To use this module, import the os module, and access this module as os.path. Working with file names This module contains a number of functions that deal with long filenames in a platform independent way. In other words, without having to deal with forward and backward slashes, colons, and whatnot. Python 3 Tutorial: 18 - Formatting. 7.1. string — Common string operations — Python v2.6.8 documentation. The string module contains a number of useful constants and classes, as well as some deprecated legacy functions that are also available as methods on strings.

7.1. string — Common string operations — Python v2.6.8 documentation

In addition, Python’s built-in string classes support the sequence type methods described in the Sequence Types — str, unicode, list, tuple, buffer, xrange section, and also the string-specific methods described in the String Methods section. 17.1. subprocess — Subprocess management — Python v2.7.5 documentation. Execute a child program in a new process.

17.1. subprocess — Subprocess management — Python v2.7.5 documentation

On Unix, the class uses os.execvp()-like behavior to execute the child program. On Windows, the class uses the Windows CreateProcess() function. Python [subprocess] 01 Call. 15.1. os — Miscellaneous operating system interfaces — Python v2.7.5 documentation. This module provides a portable way of using operating system dependent functionality.

15.1. os — Miscellaneous operating system interfaces — Python v2.7.5 documentation

If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module. Notes on the availability of these functions: Note All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. Python's OS Module. Python - What does `if __name__ == "__main__":` do. How to execute Python scripts(.py) in Linux. Tail a file in python by glenbot. Most people who have mucked around a linux shell long enough have used the tail utility to get X lines from the end of a file.

Tail a file in python by glenbot

7.1. string — Common string operations — Python v2.6.9 documentation. Python Basics. Linux - python parse file and put in for loop. Python Scripts as a Replacement for Bash Utility Scripts. For Linux users, the command line is a celebrated part of our entire experience.

Python Scripts as a Replacement for Bash Utility Scripts

Unlike other popular operating systems, where the command line is a scary proposition for all but the most experienced veterans, in the Linux community, command-line use is encouraged. Often the command line can provide a more elegant and efficient solution when compared to doing a similar task with a graphical user interface. As the Linux community has grown up with a dependence on the command line, UNIX shells, such as bash and zsh, have grown into extremely formidable tools that complement the UNIX shell experience.

With bash and other similar shells, a number of powerful features are available, such as piping, filename wild-carding and the ability to read commands from a file called a script.