Pages

Friday 12 August 2016

Python Basics for absolute Beginners

1. What is Python Programming

It is a widely used high-level, interpreted, dynamic programming language. Its design philosophy emphasizes code readability, and allows programmers to express concepts in fewer lines of code when comparing other languages such as C++ or Java.

2. How to Use it

Download latest version of Python bundle from https://www.python.org/. Installation steps are pretty simple, you need to follow the steps while installation with the default settings.

Type “python”  on your terminal, to verify your python installation. This will print Python version along with your machine details.

Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now, you can start your python coding

3. Python Syntax 

To start any block, use : instead of { and indented with same number of space to indicate code for the same block. You will see, when we go through function declaration.

4. Say Hello to Python

To print anything on terminal you need to type “print” and space and your message. Example: 
print “Hello Python”

>>> print "Hello Python”   
Hello Python
>>> 

print became function in Python version 3.x. So, you need to use 

>>> print(“Hello Python”)   
Hello Python
>>>


5. Data Types in Python

  1. String
  2. Number
  3. List
  4. Tuple
  5. Dictionary


6. Variable Declaration and Naming Conventions

A valid variable declaration would be lowercase with underscore(_) between the words and shouldn’t start with number.Example :

my_var = 4  <— valid
8my_var = 5  <— invalid

6.1. How to declare String (Immutable)

name =“AppTech Solution”
str_with_quote=“AppTech Solution’s Tutorial”
str_multi_line=“”” AppTech Solution
Welcomes You”””

6.2. How to declare Number (Immutable)

my_var = 4

6.3. How to declare List (Mutable)

It holds sequences and values are enclosed with square bracket [].

>>> a=[3,5,"Ram"]
>>> print a
[3, 5, 'Ram']
>>> 

6.4. How to declare Tuple (Immutable)

Similar to List, but values are enclosed with small bracket ().

>>> a=("alpha",34,"beta")
>>> print a
('alpha', 34, 'beta')
>>> 

6.5. How to declare Dictionary (Mutable)

Are similar to hash-map and values are enclosed with curly bracket {}.

>>> laptop={}
>>> laptop["hp"]=30000
>>> laptop["dell"]=20000
>>> laptop["acer"]=25000
>>> print laptop["dell"]
20000
>>> print laptop
{'acer': 25000, 'hp': 30000, 'dell': 20000}
>>> 


7. Functions

Function name should not start with numbers and use underscore(_) between the words. Basic syntax is

def function_name(arg):
    body with indentation
    more code here
    some more line
    return    

Example:

def addition(num):
    return num+2

my_num= addition(5)


8. Loops and Condition Controls

The syntax of a while loop in Python programming language is 

while expression :
      statement(s)

Example:

>>> a=5
>>> while(a>1): 
...    print a
...    a-=1
... 
5
4
3
2
>>>

The syntax of a for loop in Python programming language is 

for iter in sequence :
          statement(s)

Example:

>>> for letter in 'Python':     # First Example
...    print 'Current Letter :', letter
... 
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n

No comments:

Post a Comment