0% found this document useful (0 votes)
4 views

Week 02 Introduction to Python Programming- Subash

Uploaded by

Prachee Mahato
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 02 Introduction to Python Programming- Subash

Uploaded by

Prachee Mahato
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Lecture – 02

Introduction to python programming


language

Complied by: Subash Khatiwada


Users vs. Programmers
Users

• Literally someone who uses the computer or the smart-phone or the e-reader.
• Often is not an expert; he or she just owns the device and knows how it works.
• Example: Ali is an educated person, but computers are not his area of expertise

‹#›
• See computers as a set of tools - word processor, spreadsheet, map, todo list, etc.

Programmers

• Someone who understands computers from a technological point of view.


• Usually writes the code (the special language necessary to make the computer understand
commands and perform various functions)
• Earn the computer “ways” and the computer language
• Have tools to build new tools
• Write tools to automate a task
Programming basics
• code or source code: The sequence of instructions in a program.

• syntax: The set of legal structures and commands that can be


used in a particular programming language.

• output: The messages printed to the user by a program.

• console: The text box onto which output is printed.


– Some source code editors pop up the console as an external
window, and others contain their own console window.

3
5/5/2024
Python Programming
Python Programming Language was created by
Guido van Rossum. It was first released in the
early 1990s.

Its name comes from a 1970s British comedy


sketch show called Monty Python’s Flying Circus.

Python Programming
This Photo by Unknown Author is
licensed under CC BY-SA

High Level Programming Language.

4
5/5/2024
Features of Python
Easy and Simple to use
●Due to its Simple Syntax, it is easy and straight forward and much the same as
the English language.

Easy debugging and suitable for beginners


●Python program is executed one line at a time, it makes debugging easy and
portable.

Free and open source

Python Programming
●Python is freely available for everyone.

Object-Oriented Programming (OOP)


●Python supports object-oriented language and concepts of classes and objects.

5
5/5/2024
Continue…
Large sets of library and packages
●It provides a vast range of libraries for the various fields such as machine learning, web
developer, and for the scripting. Examples Pandas, Numpy, Keras, Pytorch, Django,
Matplotlib, Scikit-learn SciPy etc.

GUI Programming Support


●It is used for developing different GUI application. examples: PyQT, Tinker, Kivy

Dynamic Memory Allocation


●In python it is so simple to specify the data type of the variable.

Python Programming
Reference sites

6
5/5/2024
Why easy and simple to use?
import java.util.Scanner; Java program for adding two
public class JavaExample { numbers by taking inputs
public static void main(String[] args) { from user.
double num1, num2, sum;
Scanner sc = new Scanner(System.in);
System.out.print("Enter First Number: ");
num1 = sc.nextDouble();
System.out.print("Enter Second Number: ");
num2 = sc.nextDouble();
sc.close();

Python Programming
sum = num1 + num2;
System.out.println("Sum of "+num1+" and "+num2+" is:
"+sum);
}
}

7
5/5/2024
Python program for adding two
numbers by taking inputs from user.

a = int(input("Enter the first number:"))

b = int(input("Enter the second number:"))

sum = a + b

print("Sum =", sum)

Python Programming
8
Python is Dynamically Typed

5/5/2024
Language
a = 15
<type ‘int’>

a = 3.0

Python Programming
<type ‘float’>

9
Why Python?

Python is Compared to:

‹#›
• easy to learn, • C is much faster but
• relatively fast, much harder to use.
• Java is about as fast
• object-oriented, and slightly harder to
• strongly typed, use.
• widely used, and • Perl is slower, is as
• portable. easy to use, but is not
strongly typed.
5/5/2024
Application areas of Python
• Web Applications
• Network Applications
• Games Development
• Data Analysis Applications
• Machine Learning Applications
• Artificial Intelligence, Deep Learning, Neural Network Applications
• IOT
• Data Science

Python Programming
Hence it is called General Purpose Programming Language

11
Compiling and Interpreting
• Many languages require you to compile (translate) your program into a form that
the machine understands.

‹#›
• Python is instead directly interpreted into machine instructions.
5/5/2024
Variables
It is a container to hold data.

Provides a way of labeling data with a meaningful name, so that our


program can be understood more clearly.

Python Programming
13
5/5/2024
Rules for Naming
• The variable's first character must be an underscore or alphabet and remaining
character can have alphabets, digits, and underscore(_).

• White space and special characters (!, @, #, %, etc.) are not allowed.

• Names of variables are case-sensitive.

• A variable name must not be any reserved word or keyword.

• Variable can be of any length

Python Programming
Generally, the name of the variable should be written in lowercase.

14
5/5/2024
Variable – Python Example
Create a variable number = 10
number = 10
Replaced or reassign the value of number = 2.5
the variable.
a, b, c = 3, 3.5, “Python”
Assigning multiple values to
multiple variables:

Python Programming
a = b = c = “I love Python”
Assigning same values to multiple
variables:

15
5/5/2024
Constant
• Is a type of variable whose value cannot be changed.
• It is helpful to think of constants as containers that hold
information which cannot be changed later.
• Refer to names associated with values that never change
during a program’s execution.
• Fixed values such as numbers, letters, and strings are called
“constants” - because their value does not change.

Python Programming
• Constants are usually declared and assigned in a module

16
5/5/2024
Commenting in Python
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.

• Comments starts with a #, and Python will ignore them:

#This is a comment
print("Hello, World!")

Python Programming
17
5/5/2024
Python Data Types
Every value has a datatype, and variables can hold values.
length = 10

radius = 10.1

c = 2+ 2.3j

is_prime = False

name = "Subash"

set1 = {"Alice', 2, 3,’Bob’}

Python Programming
list1 = [1, "Subash", "Python", 40]

d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}

t = ("hi", "Python", 2)

18
5/5/2024
Python Keywords
Python keywords are unique words reserved with defined meanings and functions
that we can only apply for those functions.

False await else import pass


None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while

Python Programming
assert del global not with
async elif if or yield

19
5/5/2024
Python Operators
• Arithmetic Operators

• Comparison (Relational) Operators

• Assignment Operators

• Logical Operators

• Bitwise Operators

• Membership Operators

• Identity Operators

Python Programming
20
Arithmetic Operators

Python Programming 5/5/2024


21
5/5/2024
Comparison (relational) Operators
Compare the values on either sides of them and decide the relation among them.

Python Programming
22
5/5/2024
Logical Operators
Used to combine conditional statements

Python Programming
23
5/5/2024
Bitwise Operators
Works on bits and performs bit by bit operation

p q p&q p|q p^q


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1

Python Programming
1 1 1 1 0

24
5/5/2024
Example of Bitwise Operators
1 1 1 0 0 0 (56) 1 1 1 0 0 0 (56)
& 1 0 0 0 0 1 (33) | 1 0 0 0 0 1 (33)
1 0 0 0 0 0 (32) 1 1 1 0 0 1 (57)

1 1 1 0 0 0 (56) 1 1 1 0 0 0 (56)
^ 1 0 0 0 0 1 (33) >> 2

Python Programming
0 1 1 0 0 1 (25) 1 1 1 0 (14)

25
5/5/2024
Membership Operator
Test for membership in a sequence such as strings, lists or tuples.

Python Programming
26
5/5/2024
Identity Operator
Compare the memory locations of two objects.

Python Programming
27
Operator Precedence

Python Programming 5/5/2024


28
Understand the Code
Indentation matters to code meaning

• Block structure indicated by indentation

First assignment to a variable creates it

‹#›
• Variable types don’t need to be declared.
• Python figures out the variable types on its own.

Assignment is = and comparison is ==

For numbers + - * / % are as expected

• Special use of + for string concatenation and % for string formatting (as in C’s printf)

Logical operators are words (and, or, not) not symbols

The basic printing command is print


Interactive versus Script

Interactive
• You type directly to Python one line at a time, and it

‹#›
responds
Script
• You enter a sequence of statements (lines) into a file
using a text editor and tell Python to execute the
statements in the file
5/5/2024
Expression
A data value or set of operations to compute a value.

Example:
1+4*3

Python Programming
x = 1 + 2 * 3 - 4 / 5 ** 6 //what will be the output??

31
5/5/2024
Type Conversion
Type conversion is the process of converting data of one type to
another.

A = 192
B = “50”
C = A + int(B) // Converting str into int datatype

Python Programming
32
5/5/2024
Python Installation Guide

Python Programming
37
5/5/2024
Download and install python from
https://www.python.org/downloads/

Python Programming
38
Check Python Version

5/5/2024
python --version

Python Programming
39
PIP installation (Recommended)

5/5/2024
https://pip.pypa.io/en/stable/installation/
PIP is a package manager for Python.

Click here for installation steps

Python Programming
40
5/5/2024
Install Jupyter Notebook
Go to Jupyter Notebook Website

To install jupyter notebook open cmd and type:


pip install notebook

To run jupyter notebook, open cmd and type:

Python Programming
jupyter notebook

41
5/5/2024
Importance of IDE
IDLE is an integrated development environment (IDE).

An IDE is a bundled set of software tools, extensions for


program development. This typically includes:

1. Editor : For creating and modifying programs


2. Translator : For executing programs

Python Programming
3. Debugger : For taking control of the execution of a
program to aid in finding program errors

42
Integrated Development

5/5/2024
Environment
Most widely used IDE for python programming are:

• PyCharm Community Edition (free)

• Google Colab

• Visual Studio Code

Python Programming
• Jupyter Notebook (recommended) (Install Jupyter Notebook without Anaconda)

43
5/5/2024
Fundamental Concepts of Python
Programming

Python Programming
44
5/5/2024
First Program in Python
open terminal or cmd or python shell

OR

Python Programming
open IDLE

45
5/5/2024
How to take input from user?
input() function is used to take any input from user.

A = input(“Enter the first number”)


B = input(“Enter the second number”)

Python Programming
46
Tutorial-02: Fundamentals of

5/5/2024
Python Programming
1.Install python and IDLE.
2.Write the first program in python.
3.Take an input from user and print it.
4.Take two float numbers from user and performs
arithmetic operations

Python Programming
47
5/5/2024
Variables and Datatypes
• Python type() function prints what type of data structures are used to store the
data elements in a program.

Python Programming
48
Arithmetic Operators

Python Programming 5/5/2024


49
Relational Operators

Python Programming 5/5/2024


50
Logical Operators

Python Programming 5/5/2024


51
Bitwise Operators

Python Programming 5/5/2024


52
5/5/2024
Questions
1. Explain a few applications of Python.
2. Is Python open source or proprietary?
3. What are the features of Python? Explain.
4. What is the advantage of using Python over other
languages?
5. What is Dynamic Typing?

Python Programming
6. Does Python have data types? Explain in detail.

53
END

Python Programming 5/5/2024


54

You might also like