Python -Basics

Check Version
Interactive mode
hello world and building a sentence
variables
Accept input
Print
concatenate strings
Run python scripts
comparison operators
Python path
Python logging
Pyc files
Quotes and colons

Check Version

python -V

interactive mode

type pyhton to get to the python prompt and you will be presented with a prompt >>>

# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 + 1
2

hello world and building a sentence

a hello world program
#!/usr/bin/python
print "hello world"

Add a comma at the end of the line will make the print statement print a whitespace instead of a newline

cat a1.py
#!/usr/bin/python
output = 5
print "and currently there are",
print output,

print ""
print "and currently there are"
print output

./a1.py
and currently there are 5
and currently there are
5

variables

#!/usr/bin/python
v = 2
print v
# ./variables
2
#!/usr/bin/python
v = "world"
print "hello" + v
# ./variables2
helloworld

a variable must start with a letter i.e "1 = ..." will not work but "x1 =...." will
use raw_input for accepting text rather than input which works for numbers (although in python 3 raw_input does not work)

Accept input

#!/usr/bin/python
# note you need to enter a number
choice = input("Choose your option: ")
print choice

use raw_input for accepting text rather than input which works for numbers (although in python 3 raw_input does not work)

Print

In the example:

#!/usr/bin/python
print "a"
print "C:\some\name"
print "b"
print r"C:\some\name"

the char after the word print and before the quotation marks:

  • r - you can use raw strings by adding an r before the first quote new lines and how to avoid them in text fields
  • u -in indicates that a Unicode string is supposed to be created
  • ur -raw mode The raw mode is most useful when you have to enter lots of backslashes, as can be necessary in regular expressions.

concatenate strings

Strings can be concatenated (glued together) with the + operator, and repeated with *:

3 * "to" + "day"
'tototoday'

>>> 3 * "mo"+"day"
'momomoday
>>> 3 * ("mo"+"day")
'modaymodaymoday'

>>> a = 5
>>> print "today there are",a,"days"
today there are 5 days
>>>

Run python scripts

ways to run python
1) chmod 644 script; ./script
2) python script
3) import into python with python -m pdb script for more details see python debugger
4) to your script add (same result as 3):
import pdb
pdb.set_trace() # place where ever you want tracing to begin,

comparison operators

for use in loops/if

== If the values of two operands are equal, then the condition becomes true.
!= If values of two operands are not equal, then condition becomes true.
<> If values of two operands are not equal, then condition becomes true.
> If the value of left operand is greater than the value of right operand, then condition becomes true.
< If the value of left operand is less than the value of right operand, then condition becomes true. >= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true.
<= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. Python path

import sys
print sys.path

Python logging

info on logging https://docs.python.org/3/howto/logging.html

Pyc files

Python automatically compiles your script to compiled code, so called byte code, before running it. When a module is imported for the first time, or when the source is more recent than the current compiled file, a .pyc file containing the compiled code will usually be created in the same directory as the .py file.

Quotes and colons

if a print command won't work try using colons i.e

>>> print "das£"$232!"
File "", line 1
print "das£"$232!"
^
SyntaxError: invalid syntax
>>> print 'das£"$232!'
das£"$232!
>>>