Lectures List

Python Programming Introduction

Python is an interpreted, high-level, general-purpose programming language, Python was created by Guido van Rossum and was first released in 1991.

Python support multi-paradigm like functional imperative, object-oriented, and structured.

Python is also known as the successor of the ABC language.

Where python is used?

  • Python is used in data analysis
  • Python is used to created web applications
  • Python can be used with the database
  • Python can be used to handle complex data

Python works in multiplatform like Windows, Mac, Linux, Raspberry. Python runs on the interpreter system.

Python stable release versions:

  • Python 2.0 in 2000
  • Python 3.0 in 2008

Currently, we can use python 3.8.5.

https://www.python.org/downloads/

How to install python?

First, download python from the above-given link and start to install exe file.

When you are installing python in the window make sure to select the python path option which is used to environment variable by which installed python will accessible anywhere.

How to write and run a python program?

  • Make sure you have installed the current updated version of python on your computer.
  • Open command prompt
  • Type python –version or python –v, hit enter which will show installed python in computer
  • If your system shows the python version, it means python correctly installed in your system.
  • After that visit to desktop folder (using cd desktop) on window
  • Create a new folder named home (using md home)
  • Visit that folder (using cd home)
  • Now open any code editor (like sublime text3)
  • Write your python code and save with .py extension
  • Come back to command prompt and make sure you are in the home folder and type python filename.py

Python syntax

Make sure you have installed a python interpreter on your computer and working fine.
when you write a python program there are a number of things that are used correctly.

Python indentation

Python does not use curly braces to inclose code or block of a statement like C, C++

Python uses indentation to inclose code or block of code.

Example 1:

if(country=="USA"):

print("Welcome to USA")

Note:

The number of spaces depends upon programmer but there must be at least one space.

Example 2:

for n in range(1,11):

print(n)

Python variables

In python, we don’t need to declare a variable, python use variable in the concepts of declaring and initialize together.

as we learned on C, C++, Java, there was mandatory to declare a variable before initializing them or use variables in the program.

Declare and initialize python variables

# as can see variable declaration and initialization being together. 

country="USA"
salary=12000.89
age=40
areYouHungry=True

Note:

As you can see in the above variable declaration, we do not use a semicolon(;) to terminate a syntax like C, C++, and Java
Python does not use a semicolon (;) to terminate the line of code or syntax.

Print python variables

To print python variables print() function is used.

Example 1: String variable

country="USA"
print("My favorite country is "+country)

Note:

As you can see we use + operator to concat string message(“My favorite country is”) with the variable country.

Example 2: an integer variable

age=22
print("Your age is "+str(age)+" You are so young")

Note:we are using str() typecasting function to convert an integer variable to string integer.

Example 3: decimal variable

pi=3.14
print("PI has value"+ str(pi))

Example 4: boolean variable

areYouHungry=True
print(areYouHungry)

Print variable value without concating or using + operator, using comma(,) operator

age=22
print("Your age is ",age)

Note:when we use, comma operator there we no need to typecast variable 

Formatting output or use of string format() method to format output

product="Jeans"
quantity=6
rate=1800
total=quantity*rate
# we want to display output as “6 Jeans cost at rate 1800 is 10800”
print("{} {} cost at rate {} is {}".format(quantity,product,rate,total))

Note:

As we use of curly brackets corresponding variables values will be displayed as given in the format method.

Data types in python and comments

In python, we don’t need to specify the type of data as other programming languages like C, C++, JAVA means in other programming languages there is mandatory to declare a variable with a data type to define which type of and how much data can be store by the variable which will be used in the program.

Variable declaration in python

x=100

as we write a statement x=100 which does not specify the type of data before initializing the variable, as we give a value to a variable, type of variable will be defined, for example in the above statement we have assign 100 to a variable x so the type of variable will be an integer.

Types of data type in python

Some of the basic data types are

  • String
  • Numeric
  • bool
  • list and much more

How to know the type of variable or data?

To calculate the type of variable type() function is used.

x=100
print(type(x)) # int

y=12.5
print(type(y)) #float

z=”USA”
print(type) # string

a=True
print(a) # bool

Set data type

x=int(100)
y=float(200)
z=str(“America”)

Python comments

There are different use of a comment in python

  • To make code more readable.
  • To prevent the execution of a line of code.

Types of comment in python

  1. Single line comment
  2. Multi-line comment

Single line comment

To be ignored a line by python or comment a line of code in python pound sign(#) can be used in front of the line like given below
# sum=first_number+second_number

Multi-line comment

To be ignored multi-line code by python pound sign(#) can use in multi-line as shown below.
# sum=first_number+second_number
# average=sum/2