/  Technology   /  How To Write Your First Python Program In Just 10 Minutes
How To Write Your First Python Program In Just 10 Minutes

How To Write Your First Python Program In Just 10 Minutes

So, you want to learn python but don’t know where to start. Well you are in the right place. This tutorial will help you with some basic concepts so that you can write your first python program in just 10 minutes.

In other programming languages, you have to end the line with semicolon (;) but, in python you have to take care of indentation. (i.e. the spaces at the beginning of a code line). Comments starts with pound (#) sign which are single line and multiline comments are starts and end with triple double quotes (“””)

 

Syntax

 

# This is a single line comment
“””This is a multiline comment    
This is also a multiline comment”””

Let’s get started with print statements. For that you have to use print() function. If you want to print any string then just write it inside the double quotes (“”).

 

Syntax

 

print (“” Hello World””)

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

Let’s see another example

x = 10
y = 20
print ("The value of x and y are”, x, y)

 

OUTPUT:

How To Write Your First Python Program In Just 10 Minutes

 

 

DATATYPES

The datatypes are divided into many categories

Text Typestr
Numeric Typeint, float, complex
Sequence Typelist, tuple, range
Set Typeset, frozenset
Boolean Typebool
Mapping Typedict
Binary Typebytes, bytearray, memoryview

 

1) Text Type

Syntax

print (“” Hello World””)

OUTPUT

Hello World

 

2) Numeric Type

Syntax

x = 10
y = 20
print ("The value of x and y are”, x, y)

 OUTPUT

How To Write Your First Python Program In Just 10 Minutes

 

3) Sequence Type

a) LIST

List is a collection is ordered and changeable. It allows duplicate member. A list is defined under square brackets ([ ])

Let’s see an example

Syntax

a = ['apple',2,'sunflower',2.5]
print(a)

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

 

You can also create list by using a list () function. Let’s see an example.

Syntax

a = list([12,23,12.4,'John'])
print(a)

OUTPUT

[12, 23, 12.4, 'John']

 

You can access the items by referring to the index number. Remember the indexing in python starts from 0 (first element) and the negative indexing starts from -1 (last element). Specify the index inside the square brackets ([ ]).

 Syntax

a = ['apple',2,'sunflower',2.5,'rose','john']
print (a [1])

You will get the output as the second element in the list i.e. 2

OUTPUT

2

 

You can also specify a range of indexes by specifying where to start and where to end. It will return the new list with the specified items. You have to write the range inside the square brackets separated by a colon (:). It will consider the last element as n-1. Let’s see an example.

Syntax

a = ['apple',2,'sunflower',2.5,'rose','john']
print (a [1:3])

Here the index range is from 1 to 3 (3-1 = 2). As the indexing starts from 0 and end at n-1. Here we have accessed the elements from 2 to sunflower.

OUTPUT

[2, 'sunflower']

 

b) Tuple

Tuple is a collection which is ordered and unchangeable. In python the tuples are written inside round brackets. Let’s see an example.

Syntax

a = ('red','blue','green','yellow')
print(a)

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

 

You can access the items by referring to the index number.

Syntax

a = ('red','blue','green','yellow')
print (a [2])

OUTPUT

green

 

You can also specify a range of indexes by specifying where to start and where to end. Let’s see an example.

Syntax

a = ('red','blue','green','yellow')
print(a[2:4])

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

 

Let’s see an example of Negative indexing. Negative indexing is used if you want to start the search from end of the tuple.

Syntax

a = ('red','blue','green','yellow')
print (a [-3: -1])

As the negative indexing ranges from -3 to -1. Here -3 is included in the new tuple and -1 is excluded from the tuple. So, we got only blue and green as our output.

 OUTPUT

('blue', 'green')

 

You can also create list by using a tuple () function. Let’s see an example. Let’s see an example.

Syntax

a = tuple(['sam','john','mike'])
print(a)

 

SET

A set is a collection which is unordered and unindexed. In python sets are written under curly brackets ({}). Let’s see an example how to create a set in python.

Syntax

a = {'apple','banana','watermelon','mango'}
print(a)

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

Sets are unordered, so you cannot be sure in which order the items will appear. It will be in different order whenever you run the program.

As the sets are unordered you cannot access the items from the sets by indexing. Since it has no index.

But with the help of for loop you can access the items. You have to use another variable which iterate through the given set and print the items one by one. Let’s see an example.

Syntax

a = {'apple','banana','watermelon','mango'}
for x in a:
    print(x)

Here you must take care of the indentation. Otherwise you will get an indentation error.

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

 

Once set is created you cannot change its items, but you can add new items. To add an item to the set use, add () method.

Let’s see an example on how to add items to the set using add () method.

Syntax

a = {'apple','banana','watermelon','mango'}
a.add('grapes')
print(a)

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

 

It is also possible to make set with the help of set () constructor.

Syntax

a = set (['12','23','34'])
print(a)

OUTPUT

{'12', '23', '34'}

 

Dictionary

A dictionary is a collection which is unordered, changeable and indexed. In python dictionaries are written in curly brackets. They are in a key-value pair.

Syntax

a = {"key”:” value"}

Let’s see an example

a = {"A":"Apple","C":"Car","D":"Dog"}
print(a)

OUTPUT

How To Write Your First Python Program In Just 10 Minutes

You can access the items of the dictionary by referring to its key name inside square brackets.

Let’s see an example

Syntax

a = {"A":"Apple","C":"Car","D":"Dog"}
print(a['A'])

Here we have referred the key A so in the output we get the value associated with the key which is Apple in this case.

OUTPUT

Apple

 

You can also change the value of the specific item by referring its key. Let’s see an example.

Suppose we want to change the value of key A. Instead of apple we want to write App. Let’s see how we can achieve this with the help of code.

Syntax

a = {"A":"Apple","C":"Car","D":"Dog"}
a['A'] = 'App'
print(a)

OUTPUT

{'A': 'App', 'C': 'Car', 'D': 'Dog'}

 

Instead of using the name of key inside the square brackets there is another method known as get () which will give you the same output.

Let’s see an example of get () method

Syntax

a = {"A":"Apple","C":"Car","D":"Dog"}
a.get('A')

OUTPUT

'Apple'

With the help of the above methods you can create your first python program in just 10 minutes.

Leave a comment