python tutorial
Computer Programming

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']) 
  • Operators1https://www.tutorialspoint.com/python/index.htm

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

  • Arithmetic Operators
Python Basics
  • Logical Operators
Python Basics
  • Membership Operators
Python Basics

We’ll continue Python tutorials in further posts. To stay updated subscribe to our newsletter and our RSS feed.

If you are new to python programming, I would recommend you to go through any of the following books:

Disclaimer: If you buy from these Amazon links, I might get a tiny commission that’ll help support my blog. Thanks in advance.

Happy Programming!

P.S. Check out our new tool on our website for intelligent and cool quotes & captions for your social media as well as blogs – CAPTAGRAM

2 thoughts on “Python Tutorial for Beginners (Introduction)

Leave a Reply

Subscribe to our Newsletter

Be the first to receive the latest buzz & more!
%d bloggers like this: