As a 3+ years Python Programmer I would like to share some basics of Python, This Tutorial is for absolute beginners and who want to revise python in less time.
This tutorial gives enough understanding on Python programming language. feel free to contact me if you have any silly doubt I will be super Happy to help you out.😊😊😊😃
- 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 .
Pre-requisite:
1. Python in your machine, thats it.
(If you Don't have python in your system)
Steps to install Python:
- Go to https://www.python.org/downloads and Download it.
- Install Python in Your system. it is easy as 123!!
If you have already installed python in your system
Press Win key and open Command Prompt
- in command prompt type 'python' and press enter
Hello World in Python
>>> print("Hello World!")
Hello World!
Comments
Here is example of single line comment
single line comment start with '#'
>>> print("Hello World!") #this is single line comment
Hello World!
Here is example of multiline comments(Docstrings)
multi line comments starts with
"""This is a
multiline
Comment. It is also called docstrings"""
>>> print("Hello, World!")
Hello World!
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(x)
10
>>>print(type(x))
<class 'int'>
>>>y = 7.0
>>>print(y)
7.0
>>>print(type(y))
<class, 'float'>
>>>s = "Hello"
>>>print(s)
Hello
>>>print(type(s))
<class, 'str'>
take input from user and store it in variable
a = input('Enter Anything')
print(a)
List and indexing
>>>X=['p','r','o','b','e']
>>>print(X[1])
r
here it will print 'r' because index of 'r' is 1.(index always starts with 0)
>>>X.append('carryminati')
>>>print(X)
['p','r','o','b','e','carryminati']
>>>print(X[-1])
carryminati
>>>x = True
>>>print(type(x))
<class 'bool'>
Boolean values can be True or False
>>>S={1,1,2,2,2,3,3,3,4,4,4,4,5,6}
>>>print(S)
{1, 2, 3, 4, 5, 6}
Dictionary in Python - We will learn this later
x = {"Name":"Dev", "Age":18, "Hobbies":["Code", "Music"]}
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 mutable object's value 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
- Mutable Objects : These are of type list, dict, set . Custom classes are generally mutable.In simple words, an immutable object's value can be changed even after it is created.
# lists are mutable in python
>>>color = ["red", "blue", "green"]
>>print(color)
["red", "blue", "green"]
>>>color[0] = "pink"
>>>color[-1] = "orange"
>>>print(color)
["red", "blue", "green", "orange" ]
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
Python String Operations
>>> x = "awesome"
>>> print("Python is " + x) # Concatenation
Python is awesome
python can't add string and number together
>>>x=10
>>>print("Hello " + x)
File "<stdin>", line 1
print("Hello " + x)x=10
^
SyntaxError: invalid syntax
String multiplication
>>> my_string = "iLovePython"
>>> print(my_string * 2)
'iLovePythoniLovePython'
Convert to upper
>>> print(my_string.upper()) # Convert to upper
ILOVEPYTHON
more on string methods
>>> 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
Sets in Python
>>>S = {"apple", "banana", "cherry"}
>>>print("banana" in S)
True
>>>S={1,1,2,2,2,3,3,3,4,4,4,4,5,6}
>>>print(S)
{1, 2, 3, 4, 5, 6}
Type Casting in Python
>>> x = int(1)
>>> print(x)
1
>>> y = int(2.8)
>>> print(y)
2
>>> z = float(3)
>>> print(z)
3.0
Subset in Python
# Subset
>>>my_list=['my', 'list', 'is', 'nice']
>>> my_list[0]
'my'
>>> my_list[-3]
'list'
>>> my_list[1:3]
['list', 'is']
>>> my_list[1:]
['list', 'is', 'nice']
>>> my_list[:3]
['my', 'list', 'is']
>>> my_list[:]
['my', 'list', 'is', 'nice']
>>> my_list + my_list
>>>print(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
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()
String Operations
>>> my_string[3]
>>> my_string[4:9]
>>> my_string.upper()
>>> my_string.lower()
>>> my_string.count('w')
>>> my_string.replace('e', 'i')#will replace 'e' with 'i'
>>> my_string.strip()#remove white space from whole string
Indentation
Python indentation is a way of telling the Python interpreter that a series of statements belong to a particular block of code. In languages like C, C++, Java, we use curly braces { } to indicate the start and end of a block of code. In Python, we use space/tab as indentation to indicate the same to the compiler.
Working With Functions
Follow Indentation Rules Here(White Space Before return Statement)
>>> def myfunAdd(x,y):
... return x+y
>>> myfunAdd(5,100)
105
For Loop in Python
>>>fruits = ["Carry", "banana", "Minati"]
>>>for x in fruits:
... print(x)
carry
banana
Minati
If statement:
a = 33
b = 200
if b > a:
print("b is greater than a")
If statement, without indentation (will raise an error)
a = 55
b = 300
if b > a:
print("b is greater than a")
While Loop in python
i = 1
while i < 6:
print(i)
i += 1
Exception Handling
When an error occurs, or exception as we call it, Python will normally stop and generate an error message.These exceptions can be handled using the try statement
try:
print(x)
except:
print("An exception occurred")
Working with modules
>>>import math
>>>print(math.pi)
3.141592653589793
what's now ?
Well, There is lot more Stuff To Cover But That Is Your Home work😉!
Thank you so much for reading and good luck on your Python journey!!
As always, I welcome feedback, constructive criticism, and hearing about your projects. I can be reached on Linkedin, and also on my website.
Top comments (14)
Awesome Post. Saved a lot of time🔥
Here is why I would recommend it to beginners:
Thanks❤🙂
Hi !
I think there is an error in section "Mutable objects"
lists are mutable in python
I think, the final result is :
['pink', 'blue', 'orange']
No ?! :D
Read my words again i mentioned that lists are mutable in python!
No doubt on what you say :)
I just tell you : If i write your example line by line, the final result is false :D
Thanks 😊 I've added one photo for better understanding😃
Great article for beginners. The presentation of the material is great and all concepts discussed are well explained.
There is this line under mutable objects that I believe is a mistake. It says
"In simple words, an immutable object's value can be changed even after it is created."
I think that should be mutable instead.
Yes it was typo error for now it was fixed!thank you for your contribution🙂❤
Thanks! i feel it ❤casting is not necessary in python but when i learned python from different sources they added one same topic python casting so that's why i added casting here! python is so smart that it will automatically detect data types we don't need to specify ! Yes i left many topics like finally,pass,raise,class,continue,lambda,from,assert,del,global,not,with,async,or,yield and more but i would also like to add that i didn't miss any important topic for beginners🙂❤
This post is a quick revision basic python. Great work
Thanks 🙂
🙂
Nice set of examples, each one could become a post of its own. Nice to have them all in on post.
Great article