This course designed by keeping eyes to the armature and experienced programmer those heaving prior experience in programing they can also grab this topic as well as the armature will find very easy syntax and due to high label in nature.

     1.Syntax

Python supports the following data types:

Example:

bool = True
name = "Craig"
age = 26
pi = 3.14159
print(name + ' is ' + str(age) + ' years old.')

-> Craig is 26 years old.

 

Variable Scope: Most variables in Python are local in scope to their own function or class. For instance if you define a = 1 within a function, then a will be available within that entire function but will be undefined in the main program that calls the function. Variables defined within the main program are accessible to the main program but not within functions called by the main program.
Global Variables: Global variables, however, can be declared with the global keyword.

a = 1
b = 2
def Sum():
global a, b
b = a + b
Sum()
print(b)
-> 3

3.Statements and Expressions


Some basic Python statements include:

  • print: Output strings, integers, or any other datatype.
  • The assignment statement: Assigns a value to a variable.
  • input: Allow the user to input numbers or booleans. WARNING: input accepts your input as a command and thus can be unsafe.
  • raw_input: Allow the user to input strings. If you want a number, you can use the int or float functions to convert from a string.
  • import: Import a module into Python. Can be used as import math and all functions in math can then be called by math.sin(1.57) or alternatively from math import sin and then the sine function can be called with sin(1.57).
Examples:

print "Hello World"
print('Print works with or without parenthesis')
print("and single or double quotes")
print("Newlines can be escaped like\nthis.")
print("This text will be printed"),
print("on one line becaue of the comma.")
name = raw_input("Enter your name: ")
a = int(raw_input("Enter a number: "))
print(name + "'s number is " + str(a))
a = b = 5
a = a + 4
print a,b
9 5
Python expressions can include:
a = b = 5 #The assignment statement
b += 1 #post-increment
c = “test”
import os,math #Import the os and math modules
from math import * #Imports all functions from the math module

4.Operators and Maths

Operators:

  • Arithmetic: +, -, *, /, and % (modulus)
  • Comparison: ==, !=, <, >, <=, >=
  • Logical: and, or, not
  • Exponentiation: **
  • Execution: os.system(‘ls -l’)
    #Requires import os
Maths: Requires import math
  • Absolute Value: a = abs(-7.5)
  • Arc sine: x = asin(0.5) #returns in rads
  • Ceil (round up): print(ceil(4.2))
  • Cosine: a = cos(x) #x in rads
  • Degrees: a = degrees(asin(0.5)) #a=30
  • Exp: y = exp(x) #y=e^x
  • Floor (round down): a = floor(a+0.5)
  • Log: x = log(y); #Natural Log
    x = log(y,5); #Base-5 log
  • Log Base 10: x = log10(y)
  • Max: mx = max(1, 7, 3, 4) #7
    mx = max(arr) #max value in array
  • Min: mn = min(3, 0, -1, x) #min value
  • Powers: x = pow(y,3) #x=y^3
  • Radians: a = cos(radians(60)) #a=0.5
  • Random #: Random number functions require import random
    random.seed() #Set the seed based on the system time.
    x = random() #Random number in the range [0.0, 1.0)
    y = randint(a,b) #Random integer in the range [a, b]
  • Round: print round(3.793,1; #3.8 – rounded to 1 decimal
    a = round(3.793,0) #a=4.0
  • Sine: a = sin(1.57) #in rads
  • Square Root: x = sqrt(10) #3.16…

  • Tangent: print tan(3.14)# #in rads

5.Strings

Strings can be specified using single quotes or double quotes. Strings do not expand escape sequences unless it is defined as a raw string by placing an r before the first quote: print ‘I\’ll be back.’.
print r’The newline \n will not expand’
a = “Gators”
print “The value of a is \t” + a

-> The value of a is       Gators
If a string is not defined as raw, escapes such as \n, \r, \t, \\, and \” may be used.
Optional syntax: Strings that start and end with “”” may span multiple lines: print “””
This is an example of a string in the heredoc syntax.
This text can span multiple lines
“””
String Operators:
Concatenation is done with the + operator.
Converting to numbers is done with the casting operations:
x = 1 + float(10.5) #$x=11.5, float
x = 4 – int(“3”) #$x=1, int
You can convert to a string with the str casting function:
s = str(3.5)
name = “Lee”
print name + “‘s number is ” + str(24)Comparing Strings:
Strings can be compared with the standard operators listed above: ==, !=, <, >, <=, and >=.

String Functions:


s = "Go Gators! Come on Gators!"

 

6.Arrays


Arrays in basic Python are actually lists that can contain mixed datatype. However, the numarray module contains support for true arrays, including multi-dimensional arrays, as well as IDL-style array operations and the where function. To use arrays, you must import numarray or from numarray import *. Unfortunately, numarray generally only suports numeric arrays. Lists must be used for strings or objects. By importing numarray.strings and numarray.objects, you can convert string and object lists to arrays and use some of the numarray features, but only numeric lists are fully supported by numarray.

Array Operators:

  • Concatenation:
    • Lists: a + b
      For Lists, the + operator appends the list on the right (b) to the list on the left.
      a = [“Roberson”, “Walsh”]
      b = [“Lee”, “Humphrey”]
      -> a+b = [“Roberson”, “Walsh”, “Lee”, “Humphrey”]
    • Arrays: concatenate((a,b)[,axis])
      For arrays, use the numarry function concatenate. It also allows you to specify the axis when concatenating multi-dimensional arrays.
      b = arange(5)
      print concatenate((b, arange(6)))
      -> [0 1 2 3 4 0 1 2 3 4 5]
      b=reshape(b,5,1)
      print concatenate((b,a),axis=1)
      -> [[0 0 0 0]
      [1 0 0 0]
      [2 0 8 0]
      [3 0 0 0]
      [4 0 0 0]]
  • Equality: a == b and Inequality: a != b
    For lists, these work the same as for scalars, meaning they can be used in if statments. For arrays, they return an array containing true or false for each array element.

Array Functions: All functions but len are for arrays only

 

 

 

Leave a Reply