DEV Community

Cover image for Python Zero to Hero #Beginners⚡
vivek patel
vivek patel

Posted on • Updated on

Python Zero to Hero #Beginners⚡

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:

  1. Go to https://www.python.org/downloads and Download it.
  2. Install Python in Your system. it is easy as 123!!

If you have already installed python in your system

haha

Press Win key and open Command Prompt

haha

  • in command prompt type 'python' and press enter

Hello World in Python

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

Comments

Here is example of single line comment
single line comment start with '#'

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

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!
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(x) 
 10
Enter fullscreen mode Exit fullscreen mode
>>>print(type(x))
 <class 'int'>
Enter fullscreen mode Exit fullscreen mode
>>>y = 7.0 
>>>print(y)
 7.0
>>>print(type(y))
 <class, 'float'>
Enter fullscreen mode Exit fullscreen mode
>>>s = "Hello"
>>>print(s)
 Hello
>>>print(type(s))
 <class, 'str'>
Enter fullscreen mode Exit fullscreen mode

take input from user and store it in variable

a = input('Enter Anything') 
print(a)
Enter fullscreen mode Exit fullscreen mode

List and indexing


>>>X=['p','r','o','b','e']
>>>print(X[1])
 r
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode
>>>print(X[-1])
 carryminati
Enter fullscreen mode Exit fullscreen mode
>>>x = True
>>>print(type(x))
 <class 'bool'>
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

Dictionary in Python - We will learn this later

x = {"Name":"Dev", "Age":18, "Hobbies":["Code", "Music"]}
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  vs mutable

  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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" ]
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

String multiplication

>>> my_string = "iLovePython"
>>> print(my_string * 2)
 'iLovePythoniLovePython'
Enter fullscreen mode Exit fullscreen mode

Convert to upper

>>> print(my_string.upper()) # Convert to upper
 ILOVEPYTHON
Enter fullscreen mode Exit fullscreen mode

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
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 = float(3)
>>> print(z)
 3.0
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode
>>> 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
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')#will replace 'e' with 'i'
>>> my_string.strip()#remove white space from whole string

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For Loop in Python

>>>fruits = ["Carry", "banana", "Minati"]
>>>for x in fruits:
...    print(x)
carry
banana
Minati
Enter fullscreen mode Exit fullscreen mode

If statement:

a = 33
b = 200
if b > a:
  print("b is greater than a")
Enter fullscreen mode Exit fullscreen mode

If statement, without indentation (will raise an error)

a = 55
b = 300
if b > a:
print("b is greater than a")
Enter fullscreen mode Exit fullscreen mode

While Loop in python

i = 1
while i < 6:
  print(i)
  i += 1
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Working with modules

>>>import math
>>>print(math.pi)
 3.141592653589793
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
devmehta profile image
Dev Mehta

Awesome Post. Saved a lot of time🔥
Here is why I would recommend it to beginners:

  • All the basics are explained quite easily
  • Explanation is to the point
  • Matches up my style of learning
  • And also those memes😁
Collapse
 
vivekcodes profile image
vivek patel

Thanks❤🙂

Collapse
 
zacland profile image
Zac

Hi !
I think there is an error in section "Mutable objects"

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" ]


I think, the final result is :
['pink', 'blue', 'orange']

No ?! :D

Collapse
 
vivekcodes profile image
vivek patel

Read my words again i mentioned that lists are mutable in python!

Collapse
 
zacland profile image
Zac

No doubt on what you say :)
I just tell you : If i write your example line by line, the final result is false :D

Thread Thread
 
vivekcodes profile image
vivek patel

Thanks 😊 I've added one photo for better understanding😃

Collapse
 
clarejoyce profile image
Ngoran Clare-Joyce

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.

Collapse
 
vivekcodes profile image
vivek patel

Yes it was typo error for now it was fixed!thank you for your contribution🙂❤

Collapse
 
vivekcodes profile image
vivek patel • Edited

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🙂❤

Collapse
 
ethenhunt12 profile image
Ethen hunt

This post is a quick revision basic python. Great work

Collapse
 
vivekcodes profile image
vivek patel

Thanks 🙂

 
vivekcodes profile image
vivek patel

🙂

Collapse
 
waylonwalker profile image
Waylon Walker

Nice set of examples, each one could become a post of its own. Nice to have them all in on post.

Collapse
 
ayabouchiha profile image
Aya Bouchiha

Great article