SlideShare a Scribd company logo
Python basics
What is Python?
Python is a general-purpose high-level programming language
and is widely used among the developers’ community.
What is Python?
Python is a general-purpose high-level programming language
and is widely used among the developers’ community.
General Purpose means it can be used for multiple application
such as Data Science, Scripts, Machine Learning, Desktop
Application, Web Application, etc.
High Level Programming Language means human
understandable language (with strong abstraction from the details
of the computer.)
What is Python?
Python was developed by Guido van Rossum in the late eighties
and early nineties at the National Research Institute for
Mathematics and Computer Science in the Netherlands.
Why is it called Python?
When Guido van Rossum began implementing Python, was also
reading the published scripts from “Monty Python’s Flying Circus”,
a BBC comedy series from the 1970s. Van Rossum thought he
needed a name that was short, unique, and slightly mysterious, so
he decided to call the language Python.
Python version
Python 1.0 (1994 Jan)
Python 2.0 (2000 Oct)
Python 3.0 (2000 Dec)
Python 3.11.4 (6 June 2023)
Python does not support backward compatibility as feature in 3.0
were not available in Python 2.0
What is Python?
Python has many reasons for being popular and in demand. A few
of the reasons are mentioned below.
• Emphasis on code readability, shorter codes, ease of
writing.
• Programmers can express logical concepts intarted with
Python fewer lines of code in comparison to languages such
as C++ or Java.
• Python supports multiple programming paradigms, like
object-oriented, imperative and functional programming or
procedural.
• It provides extensive support libraries (Django for web
development, Pandas for data analytics etc.)
• Dynamically typed language(Data type is based on value
assigned)
What is Python?
The core philosophy of Python is summarized in the document
The Zen of Python (PEP 20), which includes aphorisms such as:
• Beautiful is better than ugly.
• Explicit is better than implicit.
• Simple is better than complex.
• Complex is better than complicated.
• Readability counts.
https://peps.python.org/pep-0020
Reasons to learn Python
1. Simplicity
Python is one of the easiest languages to start your journey. Also,
its simplicity does not limit your functional possibilities.
• Python is a free and open-source language
• Easy-to-learn − Python has few keywords, simple structure,
and a clearly defined syntax. This allows the student to pick up the
language quickly.
• Easy-to-read − Python code is more clearly defined and visible
to the eyes.
• Python is interpreted
• Dynamically typed
Reasons to learn Python
1. Simplicity
• Python is interpreted
It has internal compiler. First interpreter will interpret the code if
error any will notify if not it will execute.
• There are no separate compilation and execution steps like C
and C++.
• Directly run the program from the source code.
• Internally, Python converts the source code into an intermediate
form called bytecodes which is then translated into native
language of specific computer to run it.
• No need to worry about linking and loading with libraries, etc.
Reasons to learn Python
1. Simplicity
• Dynamically typed
We do not need to specify the type of the variable while
declaration.
It will implicitly decide what type of values is this and thus will
assign the type at the run time.
1. Simplicity
Reasons to learn Python
Reasons to learn Python
2. Scalability
Python is a programming language that scales very fast. Among
all available languages, Python is a leader in scaling. That means
that Python has more and more possibilities.
Saying that Python provides the best options for newbies because
there are many ways to decide the same issue.
Even if you have a team of non-Python programmers, who knows
C+ +design patterns, Python will be better for them in terms of
time needed to develop and verify code correctness.
It happens fast because you don`t spend your time to find memory
leaks, work for compilation or segmentation faults.
Reasons to learn Python
3. Libraries and Frameworks
Due to its popularity, Python has hundreds of different libraries
and frameworks which is a great addition to your development
process. They save a lot of manual time and can easily replace
the whole solution.
As a geo-scientist, you will find that many of these libraries will be
focused on data visualization, data analytics, Machine Learning,
etc.
Reasons to learn Python
4. Huge Community
Python has a powerful community. You might think that it shouldn`t
be one of the main reasons why you need to select Python. But
the truth is vice versa.
If you don`t get support from other specialists, your learning path
can be difficult. That`s why you should know that this won`t
happen with your Python learning journey.
Reasons to learn Python
5. Jobs and Salary
…
Application Areas
Getting started
Finding an Interpreter
Before we start Python programming, we need to have an
interpreter to interpret and run our programs.
• There are many interpreters available freely to run Python
scripts like IDLE (Integrated Development Environment) that
comes bundled with the Python software downloaded from
http://python.org.
Examples: Spyder, Pycharm, Jupyter Notebook, etc.
• Online interpreters like https://ide.geeksforgeeks.org that can
be used to run Python programs without installing an
interpreter.
• Anaconda (https://www.anaconda.com) – a distribution of the
Python and R programming languages for scientific computing,
that aims to simplify package management and deployment.
Getting started
Writing our first program:
Just type in the following code after you start the interpreter.
print(“Hello World")
Fundamentals of Python
Python Comments
Comments are useful information that the developers provide to
make the reader understand the source code. It explains the logic
or a part of it used in the code. There are two types of comment in
Python:
• Single line comments: Python single line comment starts with
hashtag symbol with no white spaces.
# This is Comment
Fundamentals of Python
Python Comments
Comments are useful information that the developers provide to
make the reader understand the source code. It explains the logic
or a part of it used in the code. There are two types of comment in
Python:
• Multi-line string as comment:
Python multi-line comment is a piece of text enclosed in a
delimiter (“””) on each end of the comment.
“””
This would be a multiline comment
in Python that
spans several lines,
Hanji
“””
Fundamentals of Python
Built-in types
There are many kinds of information that a computer can process,
like numbers and characters. In Python (and other programming
languages), the kinds of information the language is able to
handle are known as types. Many common types are built into
Python – for example integers, floating-point numbers and strings.
Fundamentals of Python
Built-in types
There are many kinds of information that a computer can process,
like numbers and characters. In Python (and other programming
languages), the kinds of information the language is able to
handle are known as types. Many common types are built into
Python – for example integers, floating-point numbers and strings.
Integers
An integer (int type) is a whole number such as 1, 5, 1350 or -34.
1.5 is not an integer because it has a decimal point. Numbers with
decimal points are floating-point numbers. Even 1.0 is a floating-
point number and not an integer.
Fundamentals of Python
Built-in types
There are many kinds of information that a computer can process,
like numbers and characters. In Python (and other programming
languages), the kinds of information the language is able to
handle are known as types. Many common types are built into
Python – for example integers, floating-point numbers and strings.
Floating-point numbers
Floating-point numbers (float type) are numbers with a decimal
point or an exponent (or both). Examples are 5.0, 10.24, 0.0, 12.
and .3
Fundamentals of Python
Built-in types
There are many kinds of information that a computer can process,
like numbers and characters. In Python (and other programming
languages), the kinds of information the language is able to
handle are known as types. Many common types are built into
Python – for example integers, floating-point numbers and strings.
Strings
A string (type str) is a sequence of characters.
Strings in python are surrounded by either single quotation marks,
or double quotation marks.
print("Hello")
print('Hello')
Fundamentals of Python
Exercise
Which of the following numbers are valid Python integers?
110, 1.0, 17.5, -39, -2.3, “-1”, 11.0
Fundamentals of Python
Variables
Variable is a label for a location in memory.
It can be used to hold a value.
To define a new variable in Python, we simply assign a value to a
label. For example, this is how we create a variable called count,
which contains an integer value of zero:
# define variable count
count = 0
Fundamentals of Python
Variables
Variable is a label for a location in memory.
It can be used to hold a value.
To define a new variable in Python, we simply assign a value to a
label. For example, this is how we create a variable called count,
which contains an integer value of zero:
# define variable count
count = 0
# redefine variable count
count = 2
Fundamentals of Python
Variables
Variable is a label for a location in memory.
It can be used to hold a value.
Python has some rules that you must follow when forming an
identifier:
• it may only contain letters (uppercase or lowercase), numbers
or the underscore character (_) (no spaces!).
• it may not start with a number.
• it may not be a keyword.
Fundamentals of Python
Variables
Variables in Python are not “statically typed”. We do not need to
declare variables before using them or declare their type. A
variable is created the moment we first assign a value to it.
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)
Fundamentals of Python
Operators
Operators are the main building block of any programming
language. Operators allow the programmer to perform different
kinds of operations on operands. These operators can be
categorized based upon their different functionality.
Fundamentals of Python
Operators
• Arithmetic operators:
Arithmetic operators are used
to perform mathematical
operations like addition,
subtraction, multiplication and
division.
# Examples of Arithmetic Operator
a = 9
b = 4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulus (remainder)
mod = a % b
# Exponent (power)
pwr = a ** b
Fundamentals of Python
Operators
• Arithmetic operators
Operator precedence
• Python has a specific and predictable way to
determine the order in which it performs
operations. For integer operations, the
system will first handle brackets (), then **,
then *, // and %, and finally + and -.
• If an expression contains multiple operations
which are at the same level of precedence,
like *, // and %, they will be performed in
order, either from left to right (for left-
associative operators) or from right to left
(for right-associative operators).
All these arithmetic operators are left-
associative, except for **, which is right-
associative.
# Examples of Arithmetic Operator
a = 9
b = 4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulus (remainder)
mod = a % b
# Exponent (power)
pwr = a ** b
Fundamentals of Python
Operators
• Relational Operators:
Relational operators compares
the values. It either returns
True or False according to the
condition.
• == equal to
• != not equal to
• > greater than
• < less than
• >= greater than or equal to
• <= less than or equal to
# Examples of Relational Operators
a = 13
b = 33
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Fundamentals of Python
Operators
• Logical Operators:
Logical operators perform
Logical AND, Logical OR and
Logical NOT operations.
# Examples of Logical Operator
a = True
b = False
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
Fundamentals of Python
Exercise
What are the results of the following operations and explain why:
15 + 20 * 3 = ?
13 // 2 + 3 = ?
31 + 10 // 3 = ?
20 % 7 // 3 = ?
Fundamentals of Python
Basics of data Input
• input(): This function first
takes the input from the user
and then evaluates the
expression, which means
Python automatically identifies
whether the user entered a
string or a number or list.
# input() example
name = input(“Enter your name:”)
print(name)
Fundamentals of Python
Python Indentation
Python uses indentation to highlight the blocks of code.
Whitespace is used for indentation in Python. All statements with
the same distance to the right belong to the same block of code. If
a block has to be more deeply nested, it is simply indented further
to the right. You can understand it better by looking at the
following lines of code.
# Python program showing
# indentation
university = ‘NEHU'
if university == ‘NEHU’:
print(‘Hi, NEHU student!')
else:
print(‘The university is not NEHU')
print('All set !')
Fundamentals of Python
Selection
Selection in Python is made using the two keywords ‘if’ and ‘elif’
and else (elseif)
# Python program to illustrate
# selection statement
mark = 34
if mark >= 80:
print(“Grade is A”)
elif mark >= 65:
print(“Grade is B”)
elif mark >= 50:
print(“Grade is C”)
else:
print(“Grade is D”)
Fundamentals of Python
Selection
Selection in Python is made using the two keywords ‘if’ and ‘elif’
and else (elseif)
# Python program to illustrate
# selection statement
mark = 34
if mark >= 80:
print(“Grade is A”)
elif mark >= 65:
print(“Grade is B”)
else:
if mark >= 50:
print(“Grade is C”)
else:
print(“Grade is D”)
Fundamentals of Python
Selection
Selection in Python is made using the two keywords ‘if’ and ‘elif’
and else (elseif)
# Python program to illustrate
# selection statement
mark = 34
if mark >= 80:
print(“Grade is A”)
elif mark >= 65:
print(“Grade is B”)
else:
if mark >= 50:
print(“Grade is C”)
else:
print(“Grade is D”)
Python basics

More Related Content

Python basics

  • 2. What is Python? Python is a general-purpose high-level programming language and is widely used among the developers’ community.
  • 3. What is Python? Python is a general-purpose high-level programming language and is widely used among the developers’ community. General Purpose means it can be used for multiple application such as Data Science, Scripts, Machine Learning, Desktop Application, Web Application, etc. High Level Programming Language means human understandable language (with strong abstraction from the details of the computer.)
  • 4. What is Python? Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands. Why is it called Python? When Guido van Rossum began implementing Python, was also reading the published scripts from “Monty Python’s Flying Circus”, a BBC comedy series from the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python.
  • 5. Python version Python 1.0 (1994 Jan) Python 2.0 (2000 Oct) Python 3.0 (2000 Dec) Python 3.11.4 (6 June 2023) Python does not support backward compatibility as feature in 3.0 were not available in Python 2.0
  • 6. What is Python? Python has many reasons for being popular and in demand. A few of the reasons are mentioned below. • Emphasis on code readability, shorter codes, ease of writing. • Programmers can express logical concepts intarted with Python fewer lines of code in comparison to languages such as C++ or Java. • Python supports multiple programming paradigms, like object-oriented, imperative and functional programming or procedural. • It provides extensive support libraries (Django for web development, Pandas for data analytics etc.) • Dynamically typed language(Data type is based on value assigned)
  • 7. What is Python? The core philosophy of Python is summarized in the document The Zen of Python (PEP 20), which includes aphorisms such as: • Beautiful is better than ugly. • Explicit is better than implicit. • Simple is better than complex. • Complex is better than complicated. • Readability counts. https://peps.python.org/pep-0020
  • 8. Reasons to learn Python 1. Simplicity Python is one of the easiest languages to start your journey. Also, its simplicity does not limit your functional possibilities. • Python is a free and open-source language • Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly. • Easy-to-read − Python code is more clearly defined and visible to the eyes. • Python is interpreted • Dynamically typed
  • 9. Reasons to learn Python 1. Simplicity • Python is interpreted It has internal compiler. First interpreter will interpret the code if error any will notify if not it will execute. • There are no separate compilation and execution steps like C and C++. • Directly run the program from the source code. • Internally, Python converts the source code into an intermediate form called bytecodes which is then translated into native language of specific computer to run it. • No need to worry about linking and loading with libraries, etc.
  • 10. Reasons to learn Python 1. Simplicity • Dynamically typed We do not need to specify the type of the variable while declaration. It will implicitly decide what type of values is this and thus will assign the type at the run time.
  • 11. 1. Simplicity Reasons to learn Python
  • 12. Reasons to learn Python 2. Scalability Python is a programming language that scales very fast. Among all available languages, Python is a leader in scaling. That means that Python has more and more possibilities. Saying that Python provides the best options for newbies because there are many ways to decide the same issue. Even if you have a team of non-Python programmers, who knows C+ +design patterns, Python will be better for them in terms of time needed to develop and verify code correctness. It happens fast because you don`t spend your time to find memory leaks, work for compilation or segmentation faults.
  • 13. Reasons to learn Python 3. Libraries and Frameworks Due to its popularity, Python has hundreds of different libraries and frameworks which is a great addition to your development process. They save a lot of manual time and can easily replace the whole solution. As a geo-scientist, you will find that many of these libraries will be focused on data visualization, data analytics, Machine Learning, etc.
  • 14. Reasons to learn Python 4. Huge Community Python has a powerful community. You might think that it shouldn`t be one of the main reasons why you need to select Python. But the truth is vice versa. If you don`t get support from other specialists, your learning path can be difficult. That`s why you should know that this won`t happen with your Python learning journey.
  • 15. Reasons to learn Python 5. Jobs and Salary …
  • 17. Getting started Finding an Interpreter Before we start Python programming, we need to have an interpreter to interpret and run our programs. • There are many interpreters available freely to run Python scripts like IDLE (Integrated Development Environment) that comes bundled with the Python software downloaded from http://python.org. Examples: Spyder, Pycharm, Jupyter Notebook, etc. • Online interpreters like https://ide.geeksforgeeks.org that can be used to run Python programs without installing an interpreter. • Anaconda (https://www.anaconda.com) – a distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment.
  • 18. Getting started Writing our first program: Just type in the following code after you start the interpreter. print(“Hello World")
  • 19. Fundamentals of Python Python Comments Comments are useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. There are two types of comment in Python: • Single line comments: Python single line comment starts with hashtag symbol with no white spaces. # This is Comment
  • 20. Fundamentals of Python Python Comments Comments are useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. There are two types of comment in Python: • Multi-line string as comment: Python multi-line comment is a piece of text enclosed in a delimiter (“””) on each end of the comment. “”” This would be a multiline comment in Python that spans several lines, Hanji “””
  • 21. Fundamentals of Python Built-in types There are many kinds of information that a computer can process, like numbers and characters. In Python (and other programming languages), the kinds of information the language is able to handle are known as types. Many common types are built into Python – for example integers, floating-point numbers and strings.
  • 22. Fundamentals of Python Built-in types There are many kinds of information that a computer can process, like numbers and characters. In Python (and other programming languages), the kinds of information the language is able to handle are known as types. Many common types are built into Python – for example integers, floating-point numbers and strings. Integers An integer (int type) is a whole number such as 1, 5, 1350 or -34. 1.5 is not an integer because it has a decimal point. Numbers with decimal points are floating-point numbers. Even 1.0 is a floating- point number and not an integer.
  • 23. Fundamentals of Python Built-in types There are many kinds of information that a computer can process, like numbers and characters. In Python (and other programming languages), the kinds of information the language is able to handle are known as types. Many common types are built into Python – for example integers, floating-point numbers and strings. Floating-point numbers Floating-point numbers (float type) are numbers with a decimal point or an exponent (or both). Examples are 5.0, 10.24, 0.0, 12. and .3
  • 24. Fundamentals of Python Built-in types There are many kinds of information that a computer can process, like numbers and characters. In Python (and other programming languages), the kinds of information the language is able to handle are known as types. Many common types are built into Python – for example integers, floating-point numbers and strings. Strings A string (type str) is a sequence of characters. Strings in python are surrounded by either single quotation marks, or double quotation marks. print("Hello") print('Hello')
  • 25. Fundamentals of Python Exercise Which of the following numbers are valid Python integers? 110, 1.0, 17.5, -39, -2.3, “-1”, 11.0
  • 26. Fundamentals of Python Variables Variable is a label for a location in memory. It can be used to hold a value. To define a new variable in Python, we simply assign a value to a label. For example, this is how we create a variable called count, which contains an integer value of zero: # define variable count count = 0
  • 27. Fundamentals of Python Variables Variable is a label for a location in memory. It can be used to hold a value. To define a new variable in Python, we simply assign a value to a label. For example, this is how we create a variable called count, which contains an integer value of zero: # define variable count count = 0 # redefine variable count count = 2
  • 28. Fundamentals of Python Variables Variable is a label for a location in memory. It can be used to hold a value. Python has some rules that you must follow when forming an identifier: • it may only contain letters (uppercase or lowercase), numbers or the underscore character (_) (no spaces!). • it may not start with a number. • it may not be a keyword.
  • 29. Fundamentals of Python Variables Variables in Python are not “statically typed”. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it. # An integer assignment age = 45 # A floating point salary = 1456.8 # A string name = "John" print(age) print(salary) print(name)
  • 30. Fundamentals of Python Operators Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. These operators can be categorized based upon their different functionality.
  • 31. Fundamentals of Python Operators • Arithmetic operators: Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication and division. # Examples of Arithmetic Operator a = 9 b = 4 # Addition of numbers add = a + b # Subtraction of numbers sub = a - b # Multiplication of number mul = a * b # Division(float) of number div1 = a / b # Division(floor) of number div2 = a // b # Modulus (remainder) mod = a % b # Exponent (power) pwr = a ** b
  • 32. Fundamentals of Python Operators • Arithmetic operators Operator precedence • Python has a specific and predictable way to determine the order in which it performs operations. For integer operations, the system will first handle brackets (), then **, then *, // and %, and finally + and -. • If an expression contains multiple operations which are at the same level of precedence, like *, // and %, they will be performed in order, either from left to right (for left- associative operators) or from right to left (for right-associative operators). All these arithmetic operators are left- associative, except for **, which is right- associative. # Examples of Arithmetic Operator a = 9 b = 4 # Addition of numbers add = a + b # Subtraction of numbers sub = a - b # Multiplication of number mul = a * b # Division(float) of number div1 = a / b # Division(floor) of number div2 = a // b # Modulus (remainder) mod = a % b # Exponent (power) pwr = a ** b
  • 33. Fundamentals of Python Operators • Relational Operators: Relational operators compares the values. It either returns True or False according to the condition. • == equal to • != not equal to • > greater than • < less than • >= greater than or equal to • <= less than or equal to # Examples of Relational Operators a = 13 b = 33 # a > b is False print(a > b) # a < b is True print(a < b) # a == b is False print(a == b) # a != b is True print(a != b) # a >= b is False print(a >= b) # a <= b is True print(a <= b)
  • 34. Fundamentals of Python Operators • Logical Operators: Logical operators perform Logical AND, Logical OR and Logical NOT operations. # Examples of Logical Operator a = True b = False # Print a and b is False print(a and b) # Print a or b is True print(a or b) # Print not a is False print(not a)
  • 35. Fundamentals of Python Exercise What are the results of the following operations and explain why: 15 + 20 * 3 = ? 13 // 2 + 3 = ? 31 + 10 // 3 = ? 20 % 7 // 3 = ?
  • 36. Fundamentals of Python Basics of data Input • input(): This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether the user entered a string or a number or list. # input() example name = input(“Enter your name:”) print(name)
  • 37. Fundamentals of Python Python Indentation Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code. # Python program showing # indentation university = ‘NEHU' if university == ‘NEHU’: print(‘Hi, NEHU student!') else: print(‘The university is not NEHU') print('All set !')
  • 38. Fundamentals of Python Selection Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif) # Python program to illustrate # selection statement mark = 34 if mark >= 80: print(“Grade is A”) elif mark >= 65: print(“Grade is B”) elif mark >= 50: print(“Grade is C”) else: print(“Grade is D”)
  • 39. Fundamentals of Python Selection Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif) # Python program to illustrate # selection statement mark = 34 if mark >= 80: print(“Grade is A”) elif mark >= 65: print(“Grade is B”) else: if mark >= 50: print(“Grade is C”) else: print(“Grade is D”)
  • 40. Fundamentals of Python Selection Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif) # Python program to illustrate # selection statement mark = 34 if mark >= 80: print(“Grade is A”) elif mark >= 65: print(“Grade is B”) else: if mark >= 50: print(“Grade is C”) else: print(“Grade is D”)