Friday, July 6, 2018

Python Basics - Every Programmer should Know

WHAT IS PYTHON?


Python is an interpreted high-level programming level language for general purpose programming. Created by Guido van Rossum and first released in 1991. Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales. Python features a dynamic type system and automatic memory management. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural, and has a large scale comprehensive standard library. Python interpreters are available  for many operating systems. CPython, the reference implementation of python, is open source software and has a community based development model, as do nearly all of it's variant implementations. CPython is managed by non-profit Python software foundation.

PYTHON BASICS:

Whitespace matters. Your code will not run correctly if you use improper indentation.
#this is a comment.

BASIC PYTHON LOGIC:

if: 
 
   if test:
#do stuff if test is true
   elif test 2:
#do stuff if test 2 is true 
   else:
#do stuff if both test are false

while:
 
   while test:
#keep doing stuff until test is false

for:

   for x in aSequence:
#do stuff for each member of a sequence
#for example- each item in a list

   for x in a range(10):
#do stuff 10 times(0 to 9)

   for x in range(5,10):
#do stuff 5 times(5 to 9)


PYTHON STRINGS:

A string is a sequence of characters, usually used to store text.

Creation:      the_string ="Hello world!"
                     the_string ='Hello world!'
Accessing:    the_string[4]             returns'o'
Splitting:      the_string.split(' ')    returns['Hello','world!']
                      the_string.split('r')    returns['Hello wo','ld!']

To join a list of strings together, call join() in C, uses % operator to add elements of a tuple into a string.
this_string = "there"
print "Hello %s!"%this_string returns "Hello there!"

PYTHON TUPLES:

A Tuple consist of a number of values separated by commas. They are useful for ordered pair and returning several values from a function.

Creation:    emptyTuple = ()
                   singletemTuple = ("spam",)
                   thistuple = 12, 89, 'a'
                   thistuple = (12, 89, 'a')
Accessing: thistuple[0]   returns 12

No comments:

Post a Comment