Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, August 25, 2019

Python - Command Line Arguments


Sys.argv deals with the no of arguments that will be passed through the command line arguments.The command line arguments represent all the arguments coming through the command line.The no of command line arguments start with 0.

We will write a sample code check the usefulness of this command.

We need to import the sys module for it.


Let us run the program and check the output.








Output :-








The first print statement gives us the name of the program as we are giving the argument as 0 (sys.argv[0]).

The second print statement will print the total number of arguments passed in the above program.


We should note the below points regarding command line arguments :-

  • The sys.argv takes the command line arguments in the form of a list.
  • The first element in the list is the name of the file.
  • The arguments always come in the form of a string even if we type an integer in the argument list. We need to use int() function to convert the string to integer.

Monday, July 29, 2019

Python - Keywords


Python Keywords
Keywords are the reserved words and we cannot use a keyword as variable name, function name or an Identifier. Almost 33 keywords are available in Python which is quite lesser than other languages like Java, C++ etc.

Method
Description
and
A logical operator
as
To create an alias
assert
For debugging
break
To break out of a loop
class
To define a class
continue
To continue to the next iteration of a loop
def
To define a function
del
To delete an object
elif
Used in conditional statements, same as else if
else
Used in conditional statements
except
Used with exceptions, what to do when an exception occurs
False
Boolean value, result of comparison operations
finally
Used with exceptions, a block of code that will be executed no matter if there is an exception or not
for
To create a for loop
from
To import specific parts of a module
global
To declare a global variable
if
To make a conditional statement
import
To import a module
To check if a value is present in a list, tuple, etc.
To test if two variables are equal
lambda
To create an anonymous function
None
Represents a null value
nonlocal
To declare a non-local variable
A logical operator
or
A logical operator
pass
A null statement, a statement that will do nothing
raise
To raise an exception
return
To exit a function and return a value
True
Boolean value, result of comparison operations
try
To make a try...except statement
while
To create a while loop
with
Used to simplify exception handling
yield
To end a function, returns a generator



                                                  List of Key Words

There are some basic rules that need to be followed with the keywords.No identifiers should have the same name as any of the keywords.Names that begin with __ underscore should not be used.







Saturday, July 20, 2019

Python - Datatype


Every thing in python has a datatype. There are various types of datatypes in python. Broadly they are divided into numbers and strings. Let's have a look at both.

Python Numbers

Integers, float and complex numbers fall under python’s number category. They are represented in python in following ways:

Int

Integers can be of any length, it is only limited by the memory available.

Float

A floating point number is accurate up to 15 decimal places. Integer and floating points are separated by decimal points.

Complex

Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

How would you know a data type of a variable?


type() function to know which class a variable or a value belongs.

type at work

Python Strings



String is sequence of unicode characters. We can use single quotes or double quotes to represent strings. Multiline strings can be denoted using triple quotes''' or """.

Conversion of data type

We can convert between different data types by using different type conversion functions like int(), float(), str() etc.



Python - Introduction

As per 2018 survey conducted by Stackoverflow.com , Python is the 7th most sought language and most wanted technology.The question is why python is so demanding ? Here are the few points that i have observed and gathered through books and internet.

Python is hot

According to research by Dice Python is also one of the hottest skills to have and the most popular programming language in the world based on the Popularity of Programming Language Index.

Python is interpreted

Most of the languages that are in use are compiled one , it means the language are first converted into machine code , the language of our processors before it can run.
However, python is an interpreted language and the codes are not converted
into machine code.

Python is free

The Python interpreter is developed under an OSI-approved open-source license, making it free to install, use, and distribute, even for commercial purposes.

Python is portable

Python code is interpreted and not compiled into native machine instructions, code written for one platform will work on any other platform that has the Python interpreter installed.

Python is simple

Python 3 has 33 keywords, and Python 2 has 31. By contrast, C++ has 62, Java has 53, and Visual Basic has more than 120, though these latter examples probably vary somewhat by implementation or dialect.

Friday, July 19, 2019

Python - Match vs Search


Regular Expression are very useful and can be used by developers in numerous of ways. It proved handy when we are encountering with the data having some sort of patterns. There are numerous of operation that can be done using regular expression. The re module can be used to import the regular expression module.

Match Operation in Python :-   Python match operation will look for the first matching string in a given set of data. If the searched data is not available at the first position, then it will return NONE.

Example :-  We will check what will be the output when we look for the data set that is available at the first place and when it is not at the first index position.



Python Code :- (when at the first position)
import re
data = "this is good"
res_match = re.match("this",data)
print("The result set when it is at first place",res_match)

Output :-
The result set when it is at first place <re.Match object; span=(0, 4), match='this'>

Python Code :- (when not at first position)
import re
data = "this is good"
res_mat_pos = re.match("good",data)
print("The result set when it is not in first place",res_mat_pos)
Output :- 
The result set when it is not in first place None.

Search Operation in Python :- Search operation works in the similar way as match works , except that it can search the word that are in different index position.

Sample Python Code :-
import re
data = "this is good"
result = re.search("good",data)
print("This is the result for the search",result)
Output :-
This is the result for the search <re.Match object; span=(2, 4), match='is'>

Thanks !!!

Thursday, June 6, 2019

Python - PostgreSQL DB connection


Hello friends , in this blog post we will learn how to connect python with PostgreSQL.PostgreSQL is a powerful, open source object-relational database system that uses and extends the SQL language combined with many features that safely store and scale the most complicated data workloads.PostgreSQL has earned a strong reputation for its proven architecture, reliability, data integrity, robust feature set, extensibility, and the dedication of the open source community behind the software to consistently deliver performant and innovative solutions. PostgreSQL runs on all major operating systems, has been ACID-compliant since 2001.


Step 1 : ) Create a table in PostgreSQL , here we have created a table called test.



Step 2 :) import module psycopg2 module from python repository

Type ‘pip install psycopg2’ from command line terminal

Step 3:) The required code to connect python with PostgreSQL.

import psycopg2

def connect():
conn_string = "host='localhost' dbname='postgres' user='postgres' password='postgres'"
print ("Print the connection details \n ->%s" % (conn_string))
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("SELECT * FROM test")
display_data=cursor.fetchall()
print(display_data)
cursor.close()
if __name__== '__main__':

connect()


Step 4:) We can check the output on the console.



Delta Lake - Time Travel

  Time Travel allows you to query, restore, or compare data from a previous version of a Delta table. Delta Lake automatically keeps tra...