Python Tutorial for Beginners (Introduction)

Getting Started with Python (I)

Python is an interpreted, high-level, general-purpose programming language. It was Created by Guido van Rossum in 1991. Python’s design philosophy highlights code readability with its notable use of whitespaces. Its language constructs and object-oriented approach help programmers write clean, logical code for all sorts of projects, from small scale to large codebases. It is dynamically typed and garbage-collected i.e. you don’t have to worry about memory leak. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming. And, it is case-sensitive like many other programming languages.

Prerequisites: Basic knowledge of any programming language, preferably any scripting language.

NOTE: We’ll be using Python3 based syntax in the tutorials.

To download & install: Download Python

To start Python interpreter: Run command python3 in your terminal.

Variables & Data Types

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. A variable is created the moment you first assign a value to it:

#Data types
id_no = 123            # An integer 
percentage = 95.5      # A floating point 
name = "Himanshu"      # A string  

print(id_no) 
print(percentage) 
print(name) 

Output:

#Printing Values 
123  
95.5  
Himanshu 
  • Multiple Declaration
#Assigning variables a, b, c different values
a, b, c = "Hello", 100, 43.2 

print(a) 
print(b) 
print(c) 

Output:

#Printing a, b, c
Hello 
100 
43.2 
  • Data Types

Python has five standard data types −

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

We’ve already seen Numbers and Strings, let’s see List, Tuple & Dictionary

  • List

It is a compound datatype. It can contain variables of same and different types. Lists are mutable. Lists are enclosed in “[]”.

#Lists
list1 = [100, 200, 300] 
list2 = ["abcd", 1.5, 5] 

Accessing List Elements:

#Printing Lists
print(list1[0])            #Prints first element of list 
100 

print(list1[1:3])          #Prints list elements from 2nd to 3rd 
[200, 300] 

print(list2[:-1])          #Prints list elements from start to second last 
['abcd', 1.5] 

print(list2*2)             #Makes multiple copies of list elements   
['abcd', 1.5, 5, 'abcd', 1.5, 5] 

print(list1 + list2).      #Concatenating two lists 
[100, 200, 300, 'abcd', 1.5, 5] 

These operations can also be applied to a string. For example:

#String Operations
strg = "abcdefgh" 
print(strg)          # Prints complete string 
abcdefgh 

print(strg[0])       # Prints first character of the string 
a 

print(strg[2:5])     # Prints characters starting from 3rd to 5th 
cde 

print(strg[2:])      # Prints string starting from 3rd character 
cdefgh 

print(strg*2)        # Prints string two times 
abcdefghabcdefgh 

print(strg + "ij")   # Prints concatenated string 
abcdefghij

Note: These operations won’t change the string or list unless you assign it to them.

  • Tuple

Tuples are similar to lists. But they are immutable i.e. you can’t modify them. They are read-only. Tuples are enclosed in “()”

#Tuples assignment
tup = ("abc", 50, 20.5, "efg") 

tup[0] = 1            #invalid 
TypeError: 'tuple' object does not support item assignment 

Although you can perform operations like:

#Valid operations
new_tup = tup*2 
merged_tup = tup + new_tup 

print(new_tup) 
('abc', 50, 20.5, 'efg', 'abc', 50, 20.5, 'efg') 

print(merged_tup) 
('abc', 50, 20.5, 'efg', 'abc', 50, 20.5, 'efg', 'abc', 50, 20.5, 'efg') 
  • Dictionary

Python’s dictionaries are kind of hash table type. A dictionary stores key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object. Dictionaries are enclosed by curly braces ({ })

#Dictionary operations
dict_old = {} 
dict_old['one'] = "This is one" 
dict_old[2]     = "This is two" 
 
dict_new = {'name': 'john','code':6734, 'dept': 'sales'} 
 
 
print(dict_old['one'])  
This is one       

print(dict_old[2])     
This is two        

print(dict_new.keys())   
dict_keys(['name', 'code', 'dept']) 
 
print(dict_new.values())  
dict_values(['john', 6734, 'sales']) 
  • Operators

Operators are the constructs which can manipulate the value of operands/expressions.

  • Arithmetic Operators
  • Logical Operators
  • Comparison Operators

Example:

#Code
a = 10 
if a == 10:     
  print(a) 
elif a > 10:     
  print("Greater") 
else:   
  print("Smaller")  


#Output:
10
  • Membership Operators

Example:

tup = ("a", 50, 20.5, "efg")
char = 'a'

if char in tup:
  print("char is present in tup")
else:
  print("char is not present in tup")  


#Output:
char is present in tup

We’ll continue Python tutorials in further posts.

4 thoughts on “Python Tutorial for Beginners (Introduction)

  1. I have to tell you that your blog is quickly becoming one of my favorites. You have a way of making even the most mundane topics interesting and engaging, and your humor and wit are truly infectious. Keep up the amazing work!

Leave a Reply

Your email address will not be published. Required fields are marked *