0% found this document useful (0 votes)
5 views25 pages

Python Programming Basics and Applications

Python is a high-level, interpreted programming language known for its dynamic typing and garbage collection. It is widely used in various fields such as AI, web development, and data analytics, and supports multiple programming paradigms. The document also covers Python's syntax, variable types, and data types, providing examples of basic programming concepts.

Uploaded by

itsarsc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Python Programming Basics and Applications

Python is a high-level, interpreted programming language known for its dynamic typing and garbage collection. It is widely used in various fields such as AI, web development, and data analytics, and supports multiple programming paradigms. The document also covers Python's syntax, variable types, and data types, providing examples of basic programming concepts.

Uploaded by

itsarsc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python

Python

Python is a general-purpose interpreted, interactive, object-oriented,


and high-level programming language.

Python is dynamically-typed and garbage-collected programming


language.

Like Perl, Python source code is also available under the GNU General
Public License (GPL).

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Why Python?
• AI and Machine Learning
• Data Analytics
• Data Visualization
• Programming Applications (Audio-Video Apps)
• Web development
• Game development
• Financial Analysis
• SEO

Department of Computer Science & Engineering KIPM-CET, GIDA, Gorakhpur


C C++ Java Python
#include <stdio.h> #include <iostream.h> class Simple{ print('Hello, world!')
int main() int main() public static void main(String args[])
{ { {
printf("Hello, World!"); cout << "Hello World!"; [Link]("Hello Java");
return 0; return 0; }
} } }

class Message C#
{
public: namespace HelloWorld
void display() { {
cout << "Hello World"; class Hello
} {
}; static void Main(string[] args)
int main() {
{ [Link]("Hello World!");
Message t; }
[Link](); }
return 0; }
}
Department of Computer Science & Engineering - KIPM-CET,
GIDA, Gorakhpur
Applications of Python

Department of Computer Science & Engineering KIPM-CET, GIDA, Gorakhpur


Python Programming Environment

Department of Computer Science & Engineering KIPM-CET, GIDA, Gorakhpur


• Hello World Program

• print('Hello, world!') // print “Hello World” – Python 2

• Program to add two numbers


num1 = 1.5
num2 = 6.3

# Add two numbers


sum = num1 + num2

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Department of Computer Science & Engineering KIPM-CET, GIDA, Gorakhpur


print() Function

print(*objects, sep=' ', end='\n', file=[Link], flush=False)

• objects - An object is nothing but a statement that to be printed. The * sign


represents that there can be multiple statements.
• sep - The sep parameter separates the print values. Default values is ' '.
• end - The end is printed at last in the statement.
• file - It must be an object with a write(string) method.
• flush - The stream or file is forcibly flushed if it is true. By default, its value is
false.

Department of Computer Science & Engineering KIPM-CET, GIDA, Gorakhpur


Python variables
• In programming, a variable is a container (storage area) to hold data

• Variable is a name that is used to refer to memory location. Python variable is also
known as an identifier and used to hold value.

• Python is case-sensitive. So num and Num are different variables.


For example,
• num = 5
• Num = 55
• print(num) # 5
• print(Num) # 55

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Variables Naming
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number or any special character
like $, (, * % etc.
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Python variable names are case-sensitive which means Name and
NAME are two different variables in Python.
• Python reserved keywords cannot be used naming the variable.
• Examples of valid identifiers: a123, _n, n_9, etc.
• Examples of invalid identifiers: 1a, n%4, n 9, etc.

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Variable References
The process of treating variables is somewhat different from many
other programming languages.

Python is the highly object-oriented programming language; that's why


every data item belongs to a specific type of class.

print("KIPM")
print(type("KIPM"))

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
In Python, variables are a symbolic name that is a reference or pointer to an
object.
The variables are used to denote objects by that name.
a = 50

In the above image, the variable a refers to an integer object.

Suppose we assign the integer value 50 to a new variable b


• a = 50
• b=a

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Let's assign the new value to b.
Now both variables will refer to the different objects.

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Object Identity

very created object identifies uniquely in Python.


Python provides the guaranteed that no two objects will have the same
identifier.
The built-in id() function, is used to identify the object identifier.
• a = 50
•b=a
• print(id(a))
• print(id(b))
• # Reassigned variable a
• a = 500
• print(id(a))
Department of Computer Science & Engineering - KIPM-CET,
GIDA, Gorakhpur
Department of Computer Science & Engineering - KIPM-CET,
GIDA, Gorakhpur
Python Variable Types
There are two types of variables in Python - Local variable and Global variable.

Local Variable
• Local variables are the variables that declared inside the function and have scope within
the function.
# Declaring a function
def add():
# Defining local variables. They has scope only within a function
a = 20
b = 30
c=a+b
print("The sum is:", c)

# Calling a function
add()
Department of Computer Science & Engineering - KIPM-CET,
GIDA, Gorakhpur
Global Variables
• Global variables can be used throughout the program, and its scope is in the
entire program.
• We can use global variables inside or outside the function.
• A variable declared outside the function is the global variable by default.
• Python provides the global keyword to use global variable inside the function.
• If we don't use the global keyword, the function treats it as a local variable.

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
# Declare a variable and initialize it
x = 101

# Global variable in function


def mainFunction():
# printing a global variable
global x
print(x)
# modifying a global variable
x = 'Welcome To Javatpoint'
print(x)

mainFunction()
print(x)
Department of Computer Science & Engineering - KIPM-CET,
GIDA, Gorakhpur
Python - Data Types
Python Data Types are used to define the type of a variable.
It defines what type of data we are going to store in a variable.
The data stored in memory can be of many types.

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
The data types defined in Python are given below.
• Numbers
• Sequence Type
• Boolean
• Set
• Dictionary

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Numbers
• Number stores numeric values.
• The integer, float, and complex values belong to a Python Numbers
data-type.
• Python provides the type() function to know the data-type of the
variable.
• The isinstance() function is used to check an object belongs to a
particular class.

Python creates Number objects when a number is assigned to a


variable

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Python Supports three types of numeric data:
• Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc.
Python has no restriction on the length of an integer. Its value belongs to int
• Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is
accurate upto 15 decimal points.
• complex - A complex number contains an ordered pair, i.e., x + iy where x and y
denote the real and imaginary parts, respectively. The complex numbers like
2.14j, 2.0 + 2.3j, etc.

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
Sequence Type
• The sequence data type in Python is used to store (in an organized fashion) the
sequential data.
• The sequence data type in python can be considered as a container that can store
different data

String
• A string is a series of characters.
• In Python, anything inside quotes is a string.
• We can use single, double, or triple quotes to define a string.
• The operator + is used to concatenate two strings as the operation "hello"+"
python" returns "hello python".

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur
If you don’t want it to do so, you can use raw strings by adding the letter r before the first quote. For example:

message = 'This is a string in Python'


message = "This is also a string“

And when a string contains double quotes, you can use the single quotes:
message = '“This is gorakhpur.". Uttar Pradesh‘
s = '''A multiline
string'''
print(s)
To escape the quotes, you use the backslash (\). For example:
message = 'It\'s also a valid string‘
If you don’t want it to do so, you can use raw strings by adding the letter r before
the first quote. For example:
message = r'C:\python\bin'
Department of Computer Science & Engineering - KIPM-CET,
GIDA, Gorakhpur
Creating multiline strings
• To span a string multiple lines, you use triple-quotes “””…””” or ”’…”’. For
example:
query= '''
Usage: mysql command
-h hostname
-d database name
-u username
-p password
'''
print(query)

Department of Computer Science & Engineering - KIPM-CET,


GIDA, Gorakhpur

You might also like