Friday, 9 October 2015

get used to PROGRAMMING : Python Introduction

Python is a widely used general-purpose, high-level programming language.Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java.The language provides constructs intended to enable clear programs on both a small and large scale.

Python is an interactive language that allows us to execute statements individually and see their results. In other words, you don't have to write a full code and compile it before you see the results as in other languages like C++ and Java.

print "Hello World!" 


my_string = "Hello World!"
print my_string

Let's look at Python's input reading method.

>>> name = raw_input("Hey what's your name?\n")
Hey what's your name?
Arunkumar Palaniappan
>>> print name
Arunkumar Palaniappan

That's one way of doing it for yourself. But in competitive programming we don't ask questions. We only answer them. So we will only use raw_input() to read a string without asking any question to the system.

Now, the three basic arithmetic operators are the following:

Addition (+)
Subtraction (-)
Multiplication (*)


In Python there are two kinds of divisions, namely integer division and float division.

A long time ago during the era of Python 2, when you divided an integer with another integer, no matter the result, it was always an integer.

Example

>>> 4/3 
1
And in order to make this a float division, you'd need to convert one of the arguments into a float.

Example

>>> 4/3.0
1.3333333333333333
Since Python doesn't have data types declared in advance, you never know when you want integers and when you want a float. And since floats lose precision, it's inadvisable to use them in integral calculations.

To solve this problem, future Python modules included a new type of division called integer division given by the operator //

Now / performs float division and // performs integer division.

In Python 2 we will import a module from __future__ called division.

>>> from __future__ import division
>>> print 4/3
1.3333333333333333 
>>> print 4//3
1

One of the built-in functions of Python is divmod, which takes two arguments a and b and returns a tuple containing the quotient of a/b (a//b) and remainder a.

Here a/b can be compared with integer division a//b.

>>> print divmod(177,10)
(17, 7)
Here 177/10 => 17 and 177%10 => 7

We have only heard of the powers of Python, so far; now we will witness them :)

Power or exponent in Python can be calculated using the built-in power function. Which can be called as for ab

>>> pow(a,b) 
or

>>> a**b
It's also possible to calculate abmodm.

>>> pow(a,b,m)  
This is very helpful in computations where you have to print result % mod.

Note that here a and b can be floats and even negatives; but if a third argument is present, b cannot be negative.

Integers in Python can be as big as the bytes in your machine's memory. There is no limit of 2^31−1 (c++ int) or 263−1 (C++ long long int).

Loops are control structures that iterate over a range to perform a certain task.

There are two kinds of loops in Python.

For Loop

for i in range(0,5):
    print i


While loop

i = 0
while i < 5:
    print i
    i+=1
range(0,5) returns a list of integers from 0 to 5 [0,1,2,3,4].

Lets see about Python Datatypes in another post.

No comments:

Post a Comment