An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
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:
- 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 commentHello World!
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!
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 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
- 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 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
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 > 4True
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)carrybananaMinati
If statement:
a = 33b = 200if b > a: print("b is greater than a")
If statement, without indentation (will raise an error)
a = 55b = 300if b > a:print("b is greater than a")
While Loop in python
i = 1while 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.
Original Link: https://dev.to/vivekcodes/python-zero-to-hero-beginners-5flk
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To