7 days of Python - Introduction

pandiyan

Sundarapandiyan

Posted on October 6, 2020

7 days of Python - Introduction

A word "data science" to create curiosity to me. I interested to search the internet know about that. it will create more excitement and continue to the topic. I decide to learn python, one of the most popular language. It would be nice and I believe to explore the possibilities of the python code. Python is a wide range recognized for Data Science, Machine Learning, etc.

How do I learn Python? I would see more roadmap on solid Python Foundation. So I decided to learn 7 days of python foundation.
Here the list of the python foundations:

Road Map of Python Language.

  1. Python foundations → Fundamentals, Basic Syntax, Setting up of the packages for developer uses to the corresponding project, Practice on some basic coding and little bit know the documents. Basic practice on "Edabit.com".
  2. Python paradigms → Object-Oriented and functional programming methods
  3. Python Decorators, Error Handling, Modules Generators, Debugging.
  4. File I/O, Regular Expressions, Testing, Scripting uses in python code.
  5. Finally some extra skills are known as basics → Data Scraping, Server Setup, Machine learning

Documenting the daily progress would help me build a reference journal for future reference as well. I hope this helpful resource to anyone looking to learn the language as well.

Day 1 - Introduction

Python is a high-level interpreted language and created by Guido van Rossum, and released in 1991. It was written by "C Language".

How python works and why I choose?

Python is the most popular and loves its programming language and it interpreted language which means python code has transformed(interpreted) into machine-readable code (byte-code) by another software and with the use of python virtual machine(to operate(run) byte-code).

There are different various python interpreters,

  • Cython → It's written in C language.
  • Jython → to run with java platform.
  • Pypy → It's written in R-python(Restricted Python). it faster than Cython.
  • IronPython → to target on .NET Framwork.

Each interpreted has its own pros and cons.

To Start as Beginner's

To keep on thing to any programming language as a start to simple, I wanted to enjoy to learn with some basic "Hello world!", to begin with.

I used an amazing online platform repl.it to kick start to writing code or project with python syntax

Print("Hello, world!")# this is first-line code of python 
#fethermore some code snippets
name = input("Enter the name") #Promts user input in console and store in a variable
print("welcome to world of python" + name) # prints in description with name

The building block of any programming language can be divided into the following:

  • Variables
  • Data Types
  • Functions
  • Best Practice

Today I spent understanding some basic Python terms syntax, its data types and some functions to better-known programming terms.

Variables

Variables store value. In python, these are the variable naming changes:

Variables should start with a letter(preferably small case) underscore and can be followed by numbers.

The conventional way a writing variable is used multiple words within underscore, such that first_name

Python keywords should not use in an assigning variable.

Data Types

In simple words are a way to represent values. In python has its own fundamental data types,

  • int → to represent the whole number
  • float →to represent floating real number
  • bool → to represent boolean expressions i.e True or False.
  • str → to represent strings
  • complex → to represent complex value i.e 3+5j
  • list
  • tuple
  • set
  • dict
  • none → to represent an absence of value

Today I spend time to understanding data types of the python.

Numbers

There are 3 types of numeric data types:

  • int(to represent the whole number)
  • float(to represent floating real number)
  • complex(to represent complex value i.e 3+5j)

The type function is used to determine the type of a value or an expression.

num = 100 #assign value and store variable num
print(type(num)) # <class 'int'>

num1 = 9.9
print(type(num1))# <class 'float'>

num2 = num + 100
print(type(num2))# end of the result 200 i.e. <class 'int'>

num3 = num1 / 9 
print(type(num3)) #end of the result num3 is 1.1 i.e. <class 'float'>

num4 = 2+4j 
print(type(num4)) # <class 'complex'>

num4 = complex(3,-2)
print(num4) # 3-2j
print(type(num4)) # <class 'complex'>

In Python, variable assignment happens by just writing a name and assigning a value using the = operator.

Math Functions

Python has its own built-in function. Math function that allows us to calculate various mathematical operator within the case.

Math Functions and Constant - the python documentation contains all the build-in math functions and constants

print(abs(-1)) # 1 to get absolute value
print(round(36.94)) # 37 round the number with depends on factor value i.e. less than or great than on 0.5
print(round(36.45)) # 36

import math # import math package
print(math.ceil(9.88)) # 10 to get lager whole number
print(math.floor(9.88)) # 9 to get smaller whole number
print(math.factorial(5)) # 120  factorial functions

The import module or command is used for importing a package from the python library. I will explore importing package in detail some other day.

These the math module functions with descriptions,

List of some Functions in Python Math Module

Strings

Strings in python are sequences of characters.

string1 = 'Whatever can you write anything within quote' # assign a string within single quotes
string2 = "Similarly, same as single quote" # assign a string within double quote
string3= '''This is a very long story or multiline statements''' # assign a paragraph within triple quotes
string4 = 'Hello! \\"kickstart as a Programmer\\"' # string with the escaped character sequence
print(type(string1)) #<class 'str'>
print(type(string2)) #<class 'str'>

String Concatenation

The string can be concatenated using too + operator. It simply joins or 'concatenates' strings.

first_name = "John"
last_name = "Mathew"
print(first_name + last_name)# JohnMathew 
print(first_name +' ' + last_name) #John Mathew

Type conversion

Python will throw an error if operations are performed with different types

name = "John"
age = 29
print(name + age) #TypeError: can only concatenate str (not "int") to str

#IF python concatenate, Both variable is the same data types
print(name + str(age)) # "John29"
 #or
print(int(name) + age) # ValueError: invalid literal for int() with base 10: 'John'

Here, you can see two types of error.

The first one, TypeError → variables are different data types and more depend on string.

The second one, ValueError → variables are different data types and more depend on int.

Formatted string

String formatting is a way to format a string with dynamically updating content of the output string. The first idea would be to apply string concatenation something like

first_name = 'John'
last_name = 'Mathew'
# String Concatenation
print('Welcome'+ first_name + " " + last_name + ' to enter python world!') 

# String Formatted
print(f'Welcome {first_name} {last_name} to enter the python world!') 

String Indexes

Strings in Python are simply ordered collection of characters. So we can do a lot of cool tricks with it. We can access characters of a string, select a sub-string, reverse a string and much more very easily. It is also called slicing of string.

string = "Welcome to Python World"
first_letter = string[0] # indexing start with 0
last_letter = string[-1]
print(first_letter) # W
print(last_letter) # d

#string manipulating of the indexes [start:stop:step-over]
range_1 = string[0:2] # here it starts from index 0 and ends at index 1
range_2 = string[0::1] # starts at 0, stops at end with step over 1
range_3 = string[::5] # starts at 0, till end with step 5
reverse_string =  string[::-1] # starts from end and reverses the string

print(range_1) # 'We'
print(range_2) # "Welcome to Python World"
print(range_3) # 'Wm or'
print(reverse_string)# 'dlroW nohtyP ot emocleW'

Built-in string functions and methods

Python has some built-in functions and methods to do operations on string data types. A function is generally an action that can be called independently like print() round() whereas methods are simply functions which are a part of an object and are called with a . operator.

quote = 'java was popular language'
print(len(quote)) # 21 (len calculates total no of characters)
new_quote = quote.replace('java', 'python')
print(new_quote) # python was popular language
capitalize = new_quote.capitalize()
print(capitalize) # Python was popular language
upper_case = new_quote.upper()
print(upper_case) # PYTHON WAS POPULAR LANGUAGE

print(quote) # java was popular language (Note: Strings are immutable!)

That's all for today! Still, I am interested to learn furthermore data types but I practice more python code. Because practice is well improving my coding skills. Will continue understanding the other data types and built-in methods and functions along with Boolean and List types. Pretty excited for Next Day.

Thank you for reading.

💖 💪 🙅 🚩
pandiyan
Sundarapandiyan

Posted on October 6, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related