SlideShare a Scribd company logo
Python
a programming language.
CLASS 1
What is Python?
 Python is a popular programming language.
 It was created in 1991 by Guido van Rossum.
 It is used for:
 web development (server-side),
 software development,
 mathematics,
 system scripting.
 Python can connect to database systems.
Python (cont...)
 Python works on different platforms
 Windows, Mac, Linux, Raspberry Pi, etc.
 Python has a simple syntax
 Python runs on an interpreter system
 meaning that, code can be executed as soon as it is written.
 Python can be treated in
 a procedural way,
 an object-orientated way,
 a functional way.
Python Syntax
 Python was designed for readability.
 Python uses new lines to complete a command
 as opposed to other programming languages which often use semicolons or parentheses.
 Python relies on indentation, using whitespace, to define scope; such as the scope
of loops, functions and classes.
 Other programming languages often use curly-brackets for this purpose.
Install python
 You can do it your own right?
Hello, World!
 print(“Hello, World!”)
 In terminal – command line.
 In file (saving with the extension “.py”).
Towards Programming
 In Python, the indentation is very important.
 Python uses indentation to indicate a block of code.
 Eg:
 Program
if 5 > 2:
print("Five is greater than two!")
 Output:
Error
Then what is the correct syntax?
 Program
if 5 > 2:
print("Five is greater than two!")
 Output:
Five is greater than two!
Comments
 Python has commenting capability for the purpose of in-code documentation.
 Comments start with a #, and Python will render the rest of the line as a comment:
 Eg:
#This is a comment.
print("Hello, World!")
Docstrings
 Python also has extended documentation capability, called docstrings.
 Docstrings can be one line, or multiline.
 Python uses triple quotes at the beginning and end of the docstring:
 Eg:
”””This is a
multiline docstring.”””
print("Hello, World!")
Exercise:
 Insert the missing part of the code below to output "Hello World".
 _____________(“Hello World”)
 _____This is a comment
 _____ This is a multiline comment”””
Python Variables
 Python has no command for declaring a variable
 A variable is created the moment you first assign a value to it.
 Eg:
 Program
x = 5
y = "John"
print(x)
print(y)
Output
5
John
Python Variables(cont…)
 Variables do not need to be declared with any particular type and can even change
type after they have been set.
 Eg:
Program:
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Output:
Predict the output
Variable names
 A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
 Rules for Python variables: A variable name must start with a letter or the
underscore character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
 Variable names are case-sensitive (age, Age and AGE are three different variables)
Output variables
 The Python “print” statement is often used to output variables.
 To combine both text and variable, Python uses the “+” character.
 Eg:
Program:
x = "awesome"
print("Python is " + x)
Output:
Python is awesome
Output Variables(cont…)
 You can also use the “+” character to add a variable to another variable.
 Eg:
Program:
x = "Python is "
y = "awesome"
z = x + y
print(z)
Output:
 Predict the output
Output Variables(cont…)
 For numbers, the “+” character works as a mathematical operator
 Eg:
Program:
x = 5
y = 10
print(x + y)
Output:
15
Try this…
 Program:
x = 5
y = "John"
print(x + y)
 Output:
Predict the output
Exercise:
 Create the variable named “carname” and assighn the value “Volvo” to it.
 Create a variable named “x” and assign the value 50 to it.
 Display the sum of “5+10” using two variables :”x” and “y”.
 Create a variable called “z”, assign “x+y” to it , and display the result.
Python Numbers
 There are three numeric types in Python:
 int
 float
 complex
 Variables of numeric types are created when you assign a value to them
 Eg:
x = 1 # int
y = 2.8 # float
z = 1j # complex
Python Numbers(cont…)
 To verify the type of any object in Python, use the type() function.
 Eg:
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
Python Numbers(cont…)
 Int
 Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
 Float
 Float, or "floating point number" is a number, positive or negative, containing one or
more decimals.
 Float can also be scientific numbers with an "e" to indicate the power of 10.
 Complex
 Complex numbers are written with a "j" as the imaginary part:
Python Casting
 Casting in python is therefore done using constructor functions:
 int() - constructs an integer number from an integer literal, a float literal (by rounding
down to the previous whole number), or a string literal (providing the string represents a
whole number)
 float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
 str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals
Python Casting(cont…)
 Eg: integers
x = int(1)
y = int(2.8)
z = int("3")
print(x)
print(y)
print(z)
Python Casting(cont…)
 Eg: Strings
x = str("s1")
y = str(2)
z = str(3.0)
print(x)
print(y)
print(z)
Strings
 String literals in python are surrounded by either single quotation marks, or
double quotation marks.
 ‘hello’ = “hello”
 Strings can be output to screen using the print function.
 Print(“hello”)
 Strings in Python are arrays of bytes.
 Python does not have a character data type
 a single character is simply a string with a length of 1
Strings(Cont…)
 Square brackets can be used to access elements of the string.
Eg1:
a = "Hello, World!“
print(a[1])
 Substring. Get the characters from position 2 to position 5 (not included):
Eg2:
b = "Hello, World!"
print(b[2:5])
Strings (Cont…)
 The strip() method removes any whitespace from the beginning or the end.
Eg:
a = " Hello, , World! "
print(a.strip()) # returns "Hello, World!"
 The len() method returns the length of a string
Eg:
a = "Hello, World!"
print(len(a))
Strings (cont…)
 The lower() method returns the string in lower case
Eg:
a = "Hello, World!"
print(a.lower())
 The upper() method returns the string in upper case
Eg:
a = "Hello, World!"
print(a.upper())
Strings(Cont…)
 The replace() method replaces a string with another string
Eg:
a = "Hello, World!"
print(a.replace("H", "J"))
 The split() method splits the string into substrings if it finds instances of the
separator
Eg:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
Exercise
 Method used to find the length of a string.
 Get the first character of the string txt
txt=“hello”
x=?
 Use of strip()?

More Related Content

PPTX
Python programming
PPT
Introduction to Python
PPTX
Python ppt
PPTX
Python in 30 minutes!
PPTX
Basic Python Programming: Part 01 and Part 02
PDF
Python Basics
PPTX
Programming in Python
PPTX
Intro to Python Programming Language
Python programming
Introduction to Python
Python ppt
Python in 30 minutes!
Basic Python Programming: Part 01 and Part 02
Python Basics
Programming in Python
Intro to Python Programming Language

What's hot (20)

PPTX
Python PPT
PPTX
Python basics
PDF
Introduction to Problem Solving Techniques- Python
PPTX
Python 101: Python for Absolute Beginners (PyTexas 2014)
PDF
Python File Handling | File Operations in Python | Learn python programming |...
PPT
Python ppt
PPTX
Python
PDF
Python programming : Standard Input and Output
PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
PPT
Python ppt
PPT
programming with python ppt
PPTX
Beginning Python Programming
PPTX
Python basics
PDF
Immutable vs mutable data types in python
PPTX
Python | What is Python | History of Python | Python Tutorial
PPTX
Object oriented programming in python
PDF
Bca sem 6 php practicals 1to12
PDF
Python libraries
PPTX
Introduction to Python programming
PPT
Introduction to Python
Python PPT
Python basics
Introduction to Problem Solving Techniques- Python
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python File Handling | File Operations in Python | Learn python programming |...
Python ppt
Python
Python programming : Standard Input and Output
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python ppt
programming with python ppt
Beginning Python Programming
Python basics
Immutable vs mutable data types in python
Python | What is Python | History of Python | Python Tutorial
Object oriented programming in python
Bca sem 6 php practicals 1to12
Python libraries
Introduction to Python programming
Introduction to Python
Ad

Similar to Python Basics (20)

PDF
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
PPTX
BASICS OF PYTHON usefull for the student who would like to learn on their own
PPTX
Introduction to python
PPTX
python_class.pptx
PDF
Python Programming
PPTX
Python (Data Analysis) cleaning and visualize
PPTX
Introduction to python for the abs .pptx
PDF
Sessisgytcfgggggggggggggggggggggggggggggggg
PDF
Python basics_ part1
PDF
Intro-to-Python-Part-1-first-part-edition.pdf
PPTX
Learn about Python power point presentation
PPTX
Unit -1 CAP.pptx
PPTX
Python basics
PPTX
INTRODUCTION TO PYTHON.pptx
PPTX
Introduction to Python Programming .pptx
PPTX
Presentation new
PPTX
Chapter 1 Python Revision (1).pptx the royal ac acemy
PPTX
python ppt | Python Course In Ghaziabad | Scode Network Institute
PPTX
Module-1.pptx
PPTX
Fundamentals of Python Programming
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
BASICS OF PYTHON usefull for the student who would like to learn on their own
Introduction to python
python_class.pptx
Python Programming
Python (Data Analysis) cleaning and visualize
Introduction to python for the abs .pptx
Sessisgytcfgggggggggggggggggggggggggggggggg
Python basics_ part1
Intro-to-Python-Part-1-first-part-edition.pdf
Learn about Python power point presentation
Unit -1 CAP.pptx
Python basics
INTRODUCTION TO PYTHON.pptx
Introduction to Python Programming .pptx
Presentation new
Chapter 1 Python Revision (1).pptx the royal ac acemy
python ppt | Python Course In Ghaziabad | Scode Network Institute
Module-1.pptx
Fundamentals of Python Programming
Ad

Recently uploaded (20)

PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Fundamentals of Mechanical Engineering.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Sustainable Sites - Green Building Construction
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Internet of Things (IOT) - A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Safety Seminar civil to be ensured for safe working.
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
III.4.1.2_The_Space_Environment.p pdffdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Fundamentals of Mechanical Engineering.pptx
573137875-Attendance-Management-System-original
Fundamentals of safety and accident prevention -final (1).pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Categorization of Factors Affecting Classification Algorithms Selection
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
additive manufacturing of ss316l using mig welding
Sustainable Sites - Green Building Construction
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf

Python Basics

  • 2. What is Python?  Python is a popular programming language.  It was created in 1991 by Guido van Rossum.  It is used for:  web development (server-side),  software development,  mathematics,  system scripting.  Python can connect to database systems.
  • 3. Python (cont...)  Python works on different platforms  Windows, Mac, Linux, Raspberry Pi, etc.  Python has a simple syntax  Python runs on an interpreter system  meaning that, code can be executed as soon as it is written.  Python can be treated in  a procedural way,  an object-orientated way,  a functional way.
  • 4. Python Syntax  Python was designed for readability.  Python uses new lines to complete a command  as opposed to other programming languages which often use semicolons or parentheses.  Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes.  Other programming languages often use curly-brackets for this purpose.
  • 5. Install python  You can do it your own right?
  • 6. Hello, World!  print(“Hello, World!”)  In terminal – command line.  In file (saving with the extension “.py”).
  • 7. Towards Programming  In Python, the indentation is very important.  Python uses indentation to indicate a block of code.  Eg:  Program if 5 > 2: print("Five is greater than two!")  Output: Error
  • 8. Then what is the correct syntax?  Program if 5 > 2: print("Five is greater than two!")  Output: Five is greater than two!
  • 9. Comments  Python has commenting capability for the purpose of in-code documentation.  Comments start with a #, and Python will render the rest of the line as a comment:  Eg: #This is a comment. print("Hello, World!")
  • 10. Docstrings  Python also has extended documentation capability, called docstrings.  Docstrings can be one line, or multiline.  Python uses triple quotes at the beginning and end of the docstring:  Eg: ”””This is a multiline docstring.””” print("Hello, World!")
  • 11. Exercise:  Insert the missing part of the code below to output "Hello World".  _____________(“Hello World”)  _____This is a comment  _____ This is a multiline comment”””
  • 12. Python Variables  Python has no command for declaring a variable  A variable is created the moment you first assign a value to it.  Eg:  Program x = 5 y = "John" print(x) print(y) Output 5 John
  • 13. Python Variables(cont…)  Variables do not need to be declared with any particular type and can even change type after they have been set.  Eg: Program: x = 4 # x is of type int x = "Sally" # x is now of type str print(x) Output: Predict the output
  • 14. Variable names  A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).  Rules for Python variables: A variable name must start with a letter or the underscore character  A variable name cannot start with a number  A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )  Variable names are case-sensitive (age, Age and AGE are three different variables)
  • 15. Output variables  The Python “print” statement is often used to output variables.  To combine both text and variable, Python uses the “+” character.  Eg: Program: x = "awesome" print("Python is " + x) Output: Python is awesome
  • 16. Output Variables(cont…)  You can also use the “+” character to add a variable to another variable.  Eg: Program: x = "Python is " y = "awesome" z = x + y print(z) Output:  Predict the output
  • 17. Output Variables(cont…)  For numbers, the “+” character works as a mathematical operator  Eg: Program: x = 5 y = 10 print(x + y) Output: 15
  • 18. Try this…  Program: x = 5 y = "John" print(x + y)  Output: Predict the output
  • 19. Exercise:  Create the variable named “carname” and assighn the value “Volvo” to it.  Create a variable named “x” and assign the value 50 to it.  Display the sum of “5+10” using two variables :”x” and “y”.  Create a variable called “z”, assign “x+y” to it , and display the result.
  • 20. Python Numbers  There are three numeric types in Python:  int  float  complex  Variables of numeric types are created when you assign a value to them  Eg: x = 1 # int y = 2.8 # float z = 1j # complex
  • 21. Python Numbers(cont…)  To verify the type of any object in Python, use the type() function.  Eg: x = 1 # int y = 2.8 # float z = 1j # complex print(type(x)) print(type(y)) print(type(z))
  • 22. Python Numbers(cont…)  Int  Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.  Float  Float, or "floating point number" is a number, positive or negative, containing one or more decimals.  Float can also be scientific numbers with an "e" to indicate the power of 10.  Complex  Complex numbers are written with a "j" as the imaginary part:
  • 23. Python Casting  Casting in python is therefore done using constructor functions:  int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)  float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)  str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
  • 24. Python Casting(cont…)  Eg: integers x = int(1) y = int(2.8) z = int("3") print(x) print(y) print(z)
  • 25. Python Casting(cont…)  Eg: Strings x = str("s1") y = str(2) z = str(3.0) print(x) print(y) print(z)
  • 26. Strings  String literals in python are surrounded by either single quotation marks, or double quotation marks.  ‘hello’ = “hello”  Strings can be output to screen using the print function.  Print(“hello”)  Strings in Python are arrays of bytes.  Python does not have a character data type  a single character is simply a string with a length of 1
  • 27. Strings(Cont…)  Square brackets can be used to access elements of the string. Eg1: a = "Hello, World!“ print(a[1])  Substring. Get the characters from position 2 to position 5 (not included): Eg2: b = "Hello, World!" print(b[2:5])
  • 28. Strings (Cont…)  The strip() method removes any whitespace from the beginning or the end. Eg: a = " Hello, , World! " print(a.strip()) # returns "Hello, World!"  The len() method returns the length of a string Eg: a = "Hello, World!" print(len(a))
  • 29. Strings (cont…)  The lower() method returns the string in lower case Eg: a = "Hello, World!" print(a.lower())  The upper() method returns the string in upper case Eg: a = "Hello, World!" print(a.upper())
  • 30. Strings(Cont…)  The replace() method replaces a string with another string Eg: a = "Hello, World!" print(a.replace("H", "J"))  The split() method splits the string into substrings if it finds instances of the separator Eg: a = "Hello, World!" print(a.split(",")) # returns ['Hello', ' World!']
  • 31. Exercise  Method used to find the length of a string.  Get the first character of the string txt txt=“hello” x=?  Use of strip()?