DEV Community

Cover image for The Complete Python Basics Tutorial
Dev Mehta
Dev Mehta

Posted on

The Complete Python Basics Tutorial

What is Python

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language.

Why to Learn Python

  • It supports functional and structured programming methods as well as OOP.
  • It can be used as a scripting language or can be compiled to byte-code for building large applications.
  • It provides very high-level dynamic data types and supports dynamic type checking.
  • It supports automatic garbage collection.
  • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
  • Python is Interpreted − Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.
  • Python is Interactive − You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
  • Python is Object-Oriented − Python supports Object-Oriented style or technique of programming that encapsulates code within objects.
  • Python is a Beginner's Language − Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.

Hello World in Python

>>> print("Hello World!")
Hello World!
Enter fullscreen mode Exit fullscreen mode

Comments

Here is example of single line comment

>>> print("Hello World!") #this is single line comment
Hello World!
Enter fullscreen mode Exit fullscreen mode

Here is example of multiline comment

"""This is a
multiline
Comment. It is also called docstring"""
>>> print("Hello, World!")
 Hello World!
Enter fullscreen mode Exit fullscreen mode

In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

Data Types in Python

x = 10 # print(type(x)) - Returns <class, 'int'>
# Here x is of type integer
x = 7.0 # print(type(x)) - Return <class, 'float'>
# Here x is of type float
x = "Hello"
# Here x is of type string
x = [0, 1, "hello", "world", 5.55]
# Here x is of type list
x = (0, 1, 2, 3)
# Here x is of type tuple
x = True
# Boolean
x = {"Hello", "World", "Dev"}
# Set in Python - We will learn this later
x = {"Name":"Dev", "Age":18, "Hobbies":["Code", "Music"]}
# Dictionary in Python - We will learn this later
Enter fullscreen mode Exit fullscreen mode

In programming, variables are names used to hold one or more values. Instead of repeating these values in multiple places in your code, the variable holds the results of a calculation,

Mutable and Immutable Data Types

  • Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can’t be changed after it is created.
# Tuples are immutable in python 

tuple1 = (0, 1, 2, 3)  
tuple1[0] = 4
print(tuple1) # TypeError: 'tuple' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
  • Mutable Objects : These are of type list, dict, set . Custom classes are generally mutable.
# lists are mutable in python
color = ["red", "blue", "green"] 
print(color) 

color[0] = "pink"
color[-1] = "orange" # -1 suggests the last index of the list
print(color) 
Enter fullscreen mode Exit fullscreen mode

Python Operators

x = 10
# Sum of two variables
>>> print(x+2)
 12
# x-2 Subtraction of two variables
>>> print(x-2)
8
# Multiplication of two variables
>>> print(x*2)
 20
# Exponentiation of a variable
>>> print(x**2)
 100
# Remainder of a variable
>>> print(x%2)
 0
# Division of a variable
>>> print(x/float(2))
 5.0
# Floor Division of a variable
>>> print(x//2)
 5
Enter fullscreen mode Exit fullscreen mode

Python String Operations

>>> x = "awesome"
>>> print("Python is " + x) # Concatenation
 Python is awesome
>>> my_string = "iLovePython"
>>> print(my_string * 2)
 'iLovePythoniLovePython'
>>> print(my_string.upper()) # Convert to upper
 ILOVEPYTHON
>>> print(my_string.lower()) # Convert to lower
 ilovepython
>>> print(my_string.capitalize()) # Convert to Title Case
ILovePython
>>> 'P' in my_string # Check if a character is in string
True
>>> print(my_string.swapcase()) # Swap case of string's characters
 IlOVEpTHON
>>> my_string.find('i') # Returns index of given character
 0
Enter fullscreen mode Exit fullscreen mode

Lists in Python

>>> a = 'is'
>>> b = 'nice'
>>> my_list = ['my', 'list', a, b]
>>> print(my_list)
['my', 'list', 'is', 'nice']
>>> my_list2 = [[4,5,6,7], [3,4,5,6]]
Enter fullscreen mode Exit fullscreen mode

Sets in Python

>>>S = {"apple", "banana", "cherry"}
>>>print("banana" in S)
 True
Enter fullscreen mode Exit fullscreen mode
>>>S={1,1,2,2,2,3,3,3,4,4,4,4,5,6}
>>>print(S)
 {1, 2, 3, 4, 5, 6}
Enter fullscreen mode Exit fullscreen mode

Type Casting in Python

>>> x = int(1)
>>> print(x)
1
>>> y = int(2.8)
>>> print(y)
2
>>> z = int("3")
>>> print(z)
3
>>> print(str(05))
05
Enter fullscreen mode Exit fullscreen mode

Subset in Python

 # Subset
>>> my_list[1]
>>> my_list[-3]
 Slice
>>> my_list[1:3]
>>> my_list[1:]
>>> my_list[:3]
>>> my_list[:]
 # Subset Lists of Lists
>>> my_list2[1][0]
>>> my_list2[1][:2]
Enter fullscreen mode Exit fullscreen mode
>>> my_list + my_list
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list * 2
['my', 'list', 'is', 'nice', 'my', 'list', 'is', 'nice']
>>> my_list2 > 4
True
Enter fullscreen mode Exit fullscreen mode

List Operations

>>> my_list.index(a)
>>> my_list.count(a)
>>> my_list.append('!')
>>> my_list.remove('!')
>>> del(my_list[0:1])
>>> my_list.reverse()
>>> my_list.extend('!')
>>> my_list.pop(-1)
>>> my_list.insert(0,'!')
>>> my_list.sort()
Enter fullscreen mode Exit fullscreen mode

String Operations

>>> my_string[3]
>>> my_string[4:9]
>>> my_string.upper()
>>> my_string.lower()
>>> my_string.count('w')
>>> my_string.replace('e', 'i')
>>> my_string.strip()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)