Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 2, 2020 01:09 pm GMT

Python Zero to Hero Beginners

As a 2+ 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 commentHello World!
Enter fullscreen mode Exit fullscreen mode

Here is example of multiline comments(Docstrings)
multi line comments starts with

"""This is amultilineComment. 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 Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object's value cant 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 CaseILovePython>>> 'P' in my_string # Check if a character is in stringTrue>>> 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 > 4True
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)carrybananaMinati
Enter fullscreen mode Exit fullscreen mode

If statement:

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

If statement, without indentation (will raise an error)

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

While Loop in python

i = 1while 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.


Original Link: https://dev.to/vivekcodes/python-zero-to-hero-beginners-5flk

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To