Python... Python... Part 1

Welcome everybody. For this post, I will be sharing what we learnt with our lecturer. About Python.

No!!! Not that python... but this one
Now, let us get to know about python first before going deeper.


Introduction to Python

Python is a powerful high-level, object-oriented programming language created by Guido van Rossum. Python is a fairly old language created by Guido Van Rossum. The design began in the late 1980s and was first released in February 1991.

It has simple easy-to-use syntax, making it the perfect language for someone trying to learn computer programming for the first time.

Benefits

Python is a general-purpose language. It has a wide range of applications from Web development (like Django and Bottle), scientific and mathematical computing (Orange, SymPy, NumPy) to desktop graphical user Interfaces (Pygame, Panda3D).

The syntax of the language is clean and length of the code is relatively short.

It's fun to work in Python because it allows you to think about the problem rather than focusing on the syntax.

Features of Python Programming

  1. A simple language which is easier to learn with a very simple and elegant syntax. If you are a newbie, it's a great choice to start your journey with Python.
  2. Free and open-source. You can freely use and distribute Python, even for commercial use. You can even make changes to the Python's source code.
  3. Portability. You can move Python programs from one platform to another, and run it with no problem. It runs seamlessly on almost all platforms including Windows, Mac OS X and Linux.
  4. Extensible and Embeddable. Suppose an application requires high performance. You can easily combine pieces of C/C++ or other languages with Python code. This will give your application high performance as well as scripting capabilities.
  5. A high-level interpreted language. You don't have to worry about daunting tasks like memory management, garbage collection and so on. When you run Python code, it automatically converts your code to the language your computer understands.
  6. Large standard libraries to solve common tasks. Python has a number of standard libraries which makes the life of a programmer much easier since you don't have to write all the code yourself. Standard libraries in Python are well tested and used by hundreds of people. So you can be sure that it won't break your application.
  7. Object-oriented. Object-oriented programming (OOP) helps you solve a complex problem intuitively. With OOP, you are able to divide these complex problems into smaller sets by creating objects.
Download Anaconda from https://www.continuum.io/downloads for Window /Mac.

Your First code in Python

Let's write your first Python code, (you can copy the code below for now) type the following line in jupyter notebook or in python command line terminal

print('Hello World')

When you press ENTER, you can see the output.
 
Hello World

Congratulations, you've successfully run your first Python program! Often, a program called "Hello, World!" is used to introduce a new programming language to beginners. A "Hello, World!" is a simple program that outputs "Hello, World!".

However, Python is one of the easiest language to learn, and creating "Hello, World!" program is as simple as writing print("Hello, World!"). So, we are going to write a different program.

Program to Add Two Numbers

# Add two numbers
num1 = 3
num2 = 5
sum = num1+num2
print(sum)

Line 1: # Add two numbers. Any line starting with # in Python programming is a comment. Comments are used in programming to describe the purpose of the code. This helps you as well as other programmers to understand the intent of the code. Comments are completely ignored by compilers and interpreters.

Line 2: num1 = 3

Here, num1 is a variable. You can store a value in a variable. Here, 3 is stored in this variable.

Line 3: num2 = 5

Similarly, 5 is stored in num2 variable.

Line 4: sum = num1+num2

The variables num1 and num2 are added using + operator. The result of addition is then stored in another variable sum.

Line 5: print(sum)
The print() function prints the output to the screen. In our case, it prints 8 on the screen.

The output display on screen is
 
8

Python Variables

A variable is a location in memory used to store some data (value).They are given unique names to differentiate between different memory locations. In Python, we simply assign a value to a variable and it will exist. We don't even have to declare the type of the variable. This is handled internally according to the type of value we assign to the variable. We use the assignment operator (=) to assign values to a variable. Any type of value can be assigned to any valid variable.

a = 5
b = 3.2
c = "Hello"

In Python, multiple assignments can be made in a single statement as follows:
 
a, b, c = 5, 3.2, "Hello"

If we want to assign the same value to multiple variables at once, we can do this as
 
x = y = z = 1

Data Types

There are various data types in Python. Some of the important types are listed below.

1 .Python Numbers

Integers, floating point numbers and complex numbers falls under Python numbers category. They are defined as int, float and complex class in Python.

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

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

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

Here are some examples.
 
a = 1234567890123456789
b = 0.1234567890123456789
c = 1+2j

2. String

String is sequence of Unicode characters. We can use single quotes or double quotes to represent strings. Multi-line strings can be denoted using triple quotes, ''' or " " ".
 
s = "This is a string"
s = '''a multiline

3. List

List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.

Declaring a list is straight forward. Items separated by commas are enclosed within brackets [ ].
 
a = [1, 2.2, 'python']

We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.
 
a = [5,10,15,20,25,30,35,40]

# a[2] = 15
print("a[2] = ", a[2])

Output : a[2] = 15
 
# a[0:3] = [5, 10, 15]
print("a[0:3] = ", a[0:3])

Output : a[0:3] = [5, 10, 15]
 
# a[5:] = [30, 35, 40]
print("a[5:] = ", a[5:])

Output : a[5:] = [30, 35, 40]
 
colors = ['red', 'blue', 'green']
  print colors[0]    ## red
  print colors[2]    ## green
  print len(colors)  ## 3

Output:
red
green
3

4. Dictionary

Dictionary is an unordered collection of key-value pairs.

It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value.

In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.
 
d = {1:'value','key':2}
print(type(d))

Output : <class 'dict'>
 
print("d[1] = ", d[1]);

Output : d[1] = value
 
print("d['key'] = ", d['key']);

Output : d['key'] = 2
 
# Generates error
print("d[2] = ", d[2]);

5. Set

Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.
 
a = {5,2,3,1,4}

# printing set variable
print("a = ", a)

# data type of variable a
print(type(a))

Conversion between data types

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

Output : 5.0
 
int(10.6)

Output: 10
 
str(25)

Output : '25'
 
set([1,2,3])

Output : {1, 2, 3}
 
tuple({5,6,7})

Output : (5, 6, 7)
 
list('hello')

Output : ['h', 'e', 'l', 'l', 'o']

Well, this should be more than enough for part 1. Stay tune for part 2.

Have fun playing with this programme.

Comments

Popular posts from this blog

HISTORY OF THE INTERNET

Cara nukarin 32 bit kepada dot desimal