Friday, 4 December 2015

Python interactive command line

Open command prompt (or) terminal and type ‘python3’ command. It opens python interactive session.
$ python3
Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

How to exit from python interpreter
On Windows, a Ctrl-Z gets you out of this session; on Unix, try Ctrl-D instead. Another way is simply call the quit() function to quit from python.
$ python3
Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$ 


You can type any statement (or) expressions in python interpreter, interpreter execute those statements and gives you the result.
>>> 10+20
30
>>> 20*20
400
>>> print('Hello World')
Hello World


Note:
On Unix, the Python 3.x interpreter is not installed by default with name python, so that it does not conflict with other installed Python 2.x executable.



Previous                                                 Next                                                 Home

Python: Hello World program

Open any text editor and copy the statement “print ('hello world')” and save the file name as hello.py.

hello.py
print ('hello world')

Open terminal (or) command prompt, use the command ‘python3 hello.py’ to run the file hello.py. You will get output like below

$ python3 hello.py
hello world

What happens when you instruct python to run your script?
Python first compiles your source code to byte code and sends it to python virtual machine (PVM). Byte code is the platform independent representation of your source code. PVM reads the byte code one by one and execute them.


Note:
Byte code is not machine understandable code, it is python specific representation.



Previous                                                 Next                                                 Home

Thursday, 3 December 2015

Install python on MAC OS

Step 1:Download python software from following location. I downloaded pkg file to install on mac os.

Step 2: Click the pkg file.
Press Continue.
Press Continue.
Accept license agreement and press Continue.
You can change the installation location use the button ‘Change Install Location’, and press the button Install.
Once installation is successful, you will get following screen.

Once installation is successful, open terminal and type python3 (Since I installed python 3.5).
$ python3
Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()

Use ‘quit()’ to exit from python prompt.

Location of python3
$ which python3
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

$ which python3.5
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5
On windows machines python usually placed at ‘C:\Python35’.



Previous                                                 Next                                                 Home

Why should I learn python?

Well it is almost 5 years I started my IT career, I love to work in Java, and explore open source packages. Now a day I felt bored to work in Java, so want to learn new language, one of my friend suggested me to learn python. Immediately I thought, why should I learn python?

Following are some of the reasons that forced me to learn python…(First and best reason is I am fed up in working with same language for 5 years……….:))
a.   Python is open source and is available on Windows, Mac OS X, and Unix operating systems. You can download and work with python for free.
b.   Python is fun to experiment and easy to learn. Python provides interpreter that can be used interactively, which makes it easy to experiment with features of the language.
c.    You can extend the python interpreter with new functions and data types implemented in C or C++
d.   Python improves developer productivity many times as compared to C, C++ and Java.
e.   No need to compile python program, it runs immediately after development.
f.     We can easily port python project from one platform to another (Of course Java also provides this feature)
g.   Rich in built library. Many third party open source libraries are available for logging, web development (Django — the popular open source web application framework written in Python), networking, database access etc.

h.   Python has very great community, whatever the problem you faced in python, you will get quick help.
i. You can develop applications with fewer lines of code.
 



Previous                                                 Next                                                 Home

Python Tutorial

      Why should I learn python?
      Install python on MAC OS
      How to check whether python installed or not?
      Hello World program
            Hello world application in sublime text editor
            Read username and welcome user in Python
      interactive command line
      Variables
            Boolean variables in Python
            Convert a string to boolean in Python
      Zen Of Python principles
      Operators
            Arithmetic Operators
                  Division (/) vs floored division (//) in Python
                  Use * as a string replication operator in Python
            Relational Operators
                  Understanding Numeric Comparisons in Python: How Integer and Floating-Point Equality Works
            Assignment operators
            Logical Operators
            Bitwise Operators
            Membership operators
            Identity operators
            Short circuit operators
            Multiple assignments
            Python do not have ++ (incremental), -- (decremental) operators
            Python: ternary operator
            Python: Membership operators: in, not in
      Strings
            Python: Get number of characters in a String
            Replace all the occurrences of sub string in the given string
            Change each word of string to title case
            Convert string to lower and upper case
            Insert variable value in a string
            Trim white spaces
            How to place single quotes in a string
            How to place double quotes in a string
            python: Escape \n (new line) character
            Escape special characters in python
            Formatted strings in Python
            Python: formatting strings
            Python: Define multi line strings
            TypeError: can only concatenate str (not "int") to str
            Concatenate int and string
            Split a string by space in Python
      comments
      if condition
            "Truthy" And "Falsey" values in Python
      while statement
      What values represent True
      for statement
      range function
      break statement
      continue statement
      loop exercises
      functions
            functions: return statement
            functions: Default Argument Values
            functions: Keyword arguments
            functions: Arbitrary number of arguments
            Python: Define nested function
      Lambda expressions
      Documentation strings
      lists
            Python: unpack the list to multiple variables
            Access last element of a list
            Access list elements using negative indexing
            List slicing: Select and manipulate the elements of a list
            Concatenate lists using + operator
            List replication using * operator in Python
            Iterate over the elements of a list using for loop in Python
            Length (or) number of elements in the list
            Modify element at specific index
            list: append an element
            list extend: append elements of given list
            insert: Insert an element at given position
            list remove: Remove an element
            Delete an element from a list using del statement
            list: pop: remove last element in the list
            list: clear all elements in the list
            list: index: Get index of element
            list: sort elements of list
            Sort a list but do not affect original one
            count: Count number of times element appears
            list: Reverse elements of list
            list: Copy elements of list
            in: Check for existence of element in the list
            list: del: delete elements
            Looping over lists
            Get list of numbers using range function
            Get minimum element from list of elements
            Get maximum element from list of elements
            Get sum of list of numbers
            List comprehensions
            Copy entire list
            in: check for existence of element
            not in: Check for non-existence of element in the list
      Two dimensional lists in Python
      Check list emptiness using if condition
      tuples
            Iterate over elements of a tuple
            Python: unpack the tuple to multiple variables
            Convert tuple to a list in Python
            Convert a list to tuple in Python
      Sets
      Find intersection and union of lists
      Dictionaries
            looping over dictionary
            Looping over a dictionary using keys
            Looping through the dictionary in the sorted order of keys
            Looping through all values in a dictionary
            Looping over the dictionary using items() method in Python
            List of dictionaries
            Dictionary in a dictionary
            List in a dictionary
            Check for existence of key in a dictionary using in operator in Python
            Check for existence of value in a dictionary using in operator in Python
            Python's setdefault(): A Shortcut for Adding Keys with Defaults
            Pretty print dictionary content in Python
      modules
            Executable statements in module
            import functions from module directly
            import module in other module
            Import multiple modules in one line in Python
      Command line arguments
      __pycache__ directory
      Compile all modules
      Primary secondary prompts
      get names of module
      Get names of built-in functions
      File handling
      Reading files
      Write data to file
      Append data to a file
      Get file object current position
      seek: Change file object position
      with : Predefined Clean-up Actions
      classes and objects
      built in class attributes
      Class methods in Python
      __init__ method
      Overloading __init__ method
      Class Vs Instance variables
      Destroy objects: Garbage collection
      Inheritance
      Multiple inheritance
      Python: Method overriding
      Get the doc string associated with a class
      __del()__ method
      Exceptions
            Handling Exceptions
            Raising Exceptions
            User defined exceptions
            finally clause
            Python: Print exception stack trace
            Convert a string to integer in Python
            Convert a string to float in Python
      global keyword
      Reading input
      Convert string to an integer
      Get current local encoding
      print on same line
      How to clear screen in python
      Get module name
      Get type of variable
      Generate random numbers
      Check whether an object is instance of class or not
      Get process and parent process ids
      __str__(): Print object using print method
      Python: What is None
      Python: pass keyword: execute nothing
      Python: iterators in python
      Python: Implement custom iterator
      Python: Generators, yield demo
      Python: Generator to produce infinite items
      Python: Generator expressions
      Python: assert statements
      Python: deep copy vs shallow copy
      Python: Working with namedtuple
      Python: Working with decorators
      visual studio code: How to change the python version?
      Effectively Combining Iterables with Zip function
      Iterate Over a Sequence with Indexes using enumerate method
      Data buffers in Python
      Why python3 Works but python Doesn’t (And How to Fix It)

Previous                                                 Next                                                 Home