
Introduction
Python was created by Guido van Rossum in 1991.
Python can be used to create web apps, Automated workflows, Database management tasks and Data Science. It is easy to learn because of its simple syntax and dynamic features, making it powerful 馃挭馃徎.
Python can be Objected oriented, procedural or function way.
The latest version of Python 3.X.X which I am going to use in this tutorial.
Compare to other languages like Java, c#, etc Python is easy and straightforward.
To know more about history.
Syntax
Python programs have .py as their extension and can be executed using CLI with the below commandpython hello-world.py
Indentation
if 3 > 2: print("Three is greater than two!") # This is correct
if 3 > 2: print("Three is greater than two!") # This is Not correct
Python is very strict about indentation so you always need to care about this, the number of spaces is up to you but you have to at least give one and followed with other lines.
Here don’t use curly braces “{}” instead we use “:” and indent.
Comment
comment can be used to explain your code or as a way to communicate with programmers and you can also create various documents using comments with some python module automatically.
Basically, the comment is just not read by the compiler and hence it just skips those lines.
comments start with “#” in Python.
Example:
#This is a comment
print("Hello, World!")
print("Hello, World!") #This is a comment
#print("Hello, World!")
print("Cheers, Mate!")
Variables
Variables are address spaces on the memory that store data.
In Python, we don’t require to declare a variable or give and type to a variable
Example: a = 23
Type casting is the conversion of variables to a different type like an integer to float, a string to integer and vice versa.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Datatypes
Python has different types of data types, which is a very important concept to understand in any programming language.
Python has the following data types built in by default, in these categories:
Text Type:
Numeric Types:
Sequence Types:
Mapping Type:
Set Types:
Boolean Type:
Binary Types:
None Type:
str
int, float, complex
list, tuple, range
dict
set, frozenset
bool
bytes, bytearray, memoryview
NoneType
To get the type of variable use thisprint(type(a))
In python datatype of the variable is set at the time of assignment.
x = “Hello World”
x = 20
x = 20.5
x = 1j
x = [“apple”, “banana”, “cherry”]
x = (“apple”, “banana”, “cherry”)
x = range(6)
x = {“name” : “John”, “age” : 36}
x = {“apple”, “banana”, “cherry”}
x = frozenset({“apple”, “banana”})
x = True
x = b”Hello”
x = bytearray(5)
x = memoryview(bytes(5))
x = None
str
int
float
complex
list
tuple
range
dict
set
frozenset
bool
bytes
bytearray
memoryview
NoneType
if/else
if/else is a block you will find in any language.
In Python, you can use single if, if..else, if..elif..elif..else.
If the condition is true then it goes into if block otherwise else and in the case of elif it will just the check all condition blocks until it’s finding true and if not at last goto else.
#Example1
if expression:
statement(s)
#Example2
if expression:
statement(s)
else:
statement(s)
#Example3
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Loops
There are two types of loops in Python which is For and While loop.
For Loop
This loop iterates over any sequence data like strings, lists, tuples, dictionaries, etc.
fruits = ["Apple", "Banana", "Oranges"]
#for loop
for i in fruits:
print(i)
#looping through string
for char in "student":
print(char)
While Loop
The loop will run with the condition or boolean value.
If the boolean value is false then the loop will stop.
# While loop example
a = 3
while( a < 3 ):
print("a is still smaller than 3")
print("I am out of the loop")
Output:
a is still smaller than 3
a is still smaller than 3
a is still smaller than 3
I am out of the loop
Try..Except Block
Exception Handling is also an important concept for every developer should know.
It can be very handy to prevent programs from crashing due to unexpected issues.
For example: when some user provides input wrong like for the Mobile number he gives input as a name then exception handling will give the output to the user without crashing the system or app.
There are three things you need to know for now
- Try
- Except
- Finally
Try
The try block
where you all code runs where we can catch the exception and send to except block.
Except
The except block
is the block where we handle the exception, don’t worry I will explain with an example.
Finally
The finally block
will execute the block of code every time even if you got errors.
Example
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
So this is a quick start for python, now you can create small programs on python and learn along with it, Stay tuned for more content here codeshruta.
Want to learn Javascript in easy and fast check this JavaScript A scripting language for webpages.
Pingback: JavaScript Data Types — CodeShruta
Thanks for your blog, nice to read. Do not stop.