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

Basics Type, Expressions

Uploaded by

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

Basics Type, Expressions

Uploaded by

Nishchith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 114

AICTE QIP PG CERTIFICATE PROGRAMME

on
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

Fundamentals of Python for Data Science

09-07-2024 1
09-07-2024 2
09-07-2024 3
C Lang:
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}

Java:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

09-07-2024 4
C Lang:
#include <stdio.h>
int main()
{
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);

sum = num1 + num2;


printf("Sum of the entered numbers: %d",
sum);
return 0;
}

09-07-2024 5
public class AddTwoIntegers {
public static void main(String[] args) {
int first = 10;
int second = 20;
Java Program int sum = first + second;
System.out.println("The sum is: " + sum);
}
}

09-07-2024 6
W
H
Y ?
09-07-2024 7
•Adding two numbers:
•a = 5
Hello world:
•b = 6
print(“Hello World”) •c = a + b
•print(c)

09-07-2024 8
Difference between
programming
and scripting

09-07-2024 9
Basically, all scripting languages are programming languages.

The theoretical difference between the two is that scripting languages do not
require the compilation step and are rather interpreted.

For example, normally, a C program needs to be compiled before running whereas


normally, a scripting language like JavaScript or PHP need not be compiled.

09-07-2024 10
Applications of Scripting
Languages :

1. To automate certain tasks in a program


2. Extracting information from a data set
3. Less code intensive as compared to traditional
programming languages

09-07-2024 11
• Some scripting languages traditionally used
without an explicit compilation step are
JavaScript, PHP, Python, VBScript.
• Some programming languages traditionally used
with an explicit compilation step are C, C++.

09-07-2024 12
• Python was invented by Guido
Van Rossum in 1991.

• Python is easy to learn, popular


programming language.

• Python is Free and Open source.

• Expressive Language

09-07-2024 13
Applications in
Python
• Web and Internet Development
• Applications of Python Programming
in Desktop GUI
• Science and Numeric Applications
• Software Development Application
• Python Applications in Education
• Python Applications in Business (ERP)

09-07-2024 14
Applications in
Python
• Database Access
• Network Programming
• Games and 3D Graphics
• Console-based Applications
• Computer Vision
• Machine Learning
• Robotics
• Data Analysis
09-07-2024 15
09-07-2024 16
09-07-2024 17
09-07-2024 18
09-07-2024 19
09-07-2024 20
09-07-2024 21
INTRODUCTION TO PYTHON

Basic blocks Expressions


• Input/output • Type conversion
• Variables • Mixed data types
• String basics • Floating-point errors
• Number basics • Dividing integers
• Error messages • The math module
• Comments

09-07-2024 22
Input/output
• Basic Output: Print ()

09-07-2024 23
Variables

09-07-2024 24
09-07-2024 25
09-07-2024 26
09-07-2024 27
09-07-2024 28
09-07-2024 29
09-07-2024 30
09-07-2024 31
09-07-2024 32
09-07-2024 33
09-07-2024 34
Data type

09-07-2024 35
String basics

09-07-2024 36
09-07-2024 37
09-07-2024 38
09-07-2024 39
09-07-2024 40
09-07-2024 41
09-07-2024 42
09-07-2024 43
09-07-2024 44
Number basics

09-07-2024 45
09-07-2024 46
09-07-2024 47
09-07-2024 48
09-07-2024 49
09-07-2024 50
09-07-2024 51
09-07-2024 52
Sequence Types
Sequences are containers that hold objects
• Finite, ordered, indexed by integers
1. Tuple: (1, “a”, [100], “foo”)
• An immutable ordered sequence of items
• Items can be of mixed types, including collection types
2. Strings: “foo bar”
• An immutable ordered sequence of chars
• Conceptually very much like a tuple
3. List: [“one”, “two”, 3]
• A Mutable ordered sequence of items of mixed types

09-07-2024 53
Similar Syntax
• All three sequence types (tuples, strings, and lists) share
much of the same syntax and functionality.
• Key difference:
• Tuples and strings are immutable
• Lists are mutable

09-07-2024 54
Sequence Types
• Define tuples using parentheses and commas
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
• Define lists are using square brackets and commas
>>> li = [“abc”, 34, 4.34, 23]
• Define strings using quotes (“, ‘, or “””).
>>> st = “Hello World”
>>> st = ‘Hello World’
>>> st = “””This is a multi-line
string that uses triple quotes.”””

09-07-2024 55
Sequence Types
• Access individual members of a tuple, list, or string
using square bracket “array” notation
• Note that all are 0 based…
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> tu[1] # Second item in the tuple.
‘abc’

>>> li = [“abc”, 34, 4.34, 23]


>>> li[1] # Second item in the list.
34

>>> st = “Hello World”


>>> st[1] # Second character in string.
‘e’
09-07-2024 56
Array
• Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.

09-07-2024 57
09-07-2024 58
Operators

09-07-2024 59
09-07-2024 60
09-07-2024 61
09-07-2024 62
09-07-2024 63
09-07-2024 64
Comparison operators

09-07-2024 65
Logical operators

09-07-2024 66
Bitwise operators

09-07-2024 67
Assignment operators

09-07-2024 68
Membership operators

09-07-2024 69
How to read errors?

09-07-2024 70
09-07-2024 71
Comments

09-07-2024 72
09-07-2024 73
09-07-2024 74
09-07-2024 75
Expressions
• An expression represents a single value to be computed.
• Ex: The expression 3*x - 5 evaluates to 7 when x is 4.
• Expressions are often a combination of literals, variables, and operators.
• In the previous example, 3 and 5 are literals, x is a variable, and * and - are
operators.
• Still expressions - 1 * 2 and "Hi " + "there"

09-07-2024 76
Type conversion
1. Implicit type conversion
• Common operations update a variable such that the variable's data
type needs to be changed.
• Ex: A GPS first assigns distance with 250, an integer. After a wrong
turn, the GPS assigns distance with 252.5, a float.
• The Python interpreter uses implicit type conversion to automatically
convert one data type to another.
• Once distance is assigned with 252.5, the interpreter will convert
distance from an integer to a float without the programmer needing
to specify the conversion.
09-07-2024 77
09-07-2024 78
09-07-2024 79
09-07-2024 80
09-07-2024 81
2. Explicit type conversion
• A programmer often needs to change data types to perform an
operation.
Ex: A program should read in two values using input() and sum the
values. Remember input() reads in values as strings. A programmer
can use explicit type conversion to convert one data type to another.

• int() converts a data type to an integer. Any fractional part is


removed. Ex: int(5.9) produces 5.
• float() converts a data type to a float. Ex: float(2) produces 2.0.
• str() converts a data type to a string. Ex: str(3.14) produces "3.14".

09-07-2024 82
09-07-2024 83
09-07-2024 84
09-07-2024 85
09-07-2024 86
09-07-2024 87
09-07-2024 88
09-07-2024 89
09-07-2024 90
09-07-2024 91
09-07-2024 92
09-07-2024 93
09-07-2024 94
Combining numeric types and strings
• Easy type conversion in Python can lead a programmer to assume that any
data type can be combined with another.
• Ex: Noor's program reads in a number from input and uses the number in
a calculation. This results in an error in the program because the input()
function by default stores the number as a string.
• Strings and numeric data types are incompatible for addition, subtraction,
and division. One of the operands needs to be explicitly converted
depending on the goal of arithmetic or string concatenation.

• The * operator also serves as the repetition operator, which accepts a


string operand and an integer operand and repeats the string. Ex: "banjo" *
3 produces "banjobanjobanjo".
09-07-2024 95
09-07-2024 96
09-07-2024 97
09-07-2024 98
Print n times
• Write a program that reads in two strings, str1 and str2, and an
integer, count. Concatenate the two strings with a space in between
and a newline ("\n") at the end. Print the resulting string count times.

09-07-2024 99
Floating-point errors
• Floating-point values are stored as binary by Python. The conversion
of a floating point number to the underlying binary results in specific
types of floating-point errors.
• A round-off error occurs when floating-point values are stored
erroneously as an approximation. The difference between an
approximation of a value used in computation and the correct (true)
value is called a round-off error.
• Ex: Storing the float (0.1)10 results in binary values that actually
produce(0.10000000000000000555111512312578270211815834045
41015625)10 when converted back, which is not exactly equal to
(0.1)10.

09-07-2024 100
Floating point round() function
• Python's round() function is used to round a floating-point number to a
given number of decimal places. The function requires two arguments.
• The first argument is the number to be rounded.
• The second argument decides the number of decimal places to which the
number is rounded. If the second argument is not provided, the number
will be rounded to the closest integer.
• The round() function can be used to mitigate floating-point errors.

• Ex:
• round(2.451, 2) = 2.45
• round(2.451) = 2

09-07-2024 101
09-07-2024 102
09-07-2024 103
09-07-2024 104
Dividing integers
Division and modulo
• Python provides two ways to divide numbers:
• True division (/) converts numbers to floats before dividing.
Ex: 7 / 4 becomes 7.0 / 4.0, resulting in 1.75.
• Floor division (//) computes the quotient, or the number of times
divided.
Ex: 7 // 4 is 1 because 4 goes into 7 one time, remainder 3.
• The modulo operator (%) computes the remainder. Ex: 7 % 4 is 3.

09-07-2024 105
Importing modules

• Python comes with an extensive standard library of modules.


A module is previously written code that can be imported in a program.
• The import statement defines a variable for accessing code in a module.
Import statements often appear at the beginning of a program.

• The standard library also defines built-in functions such as print(), input(),
and float().
• A built-in function is always available and does not need to be imported.

09-07-2024 106
Math Module
• A commonly used module in the standard library is the math module.
This module defines functions such as sqrt() (square root). To call
sqrt(), a program must import math and use the resulting math
variable followed by a dot.
• Ex: math.sqrt(25) evaluates to 5.0.

09-07-2024 107
Mathematical functions

09-07-2024 108
09-07-2024 109
09-07-2024 110
• Factorial of a number? Factorial ()

• GCD of x, y? Gcd()

• Euclidean distance between two points? Dist()

09-07-2024 111
09-07-2024 112
09-07-2024 113
Write the code for the quadratic formula in
the program below.

09-07-2024 114

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy