You are on page 1of 48

WHAT IS PYTHON?

ABOUT PYTHON
 Python is a popular programming language.
 Created by Guido Van Rossum, and released
in 1991.
Used for :-
 Web development (server-side),
 Software development,
 Mathematics
 System scripting.
APPLICATIONS OF PYTHON
 Python can be used on a server to create
web applications.
 Python can be used alongside software to
create workflows.
 Python can connect to database systems. It
can also read and modify files.
 Python can be used to handle big data and
perform complex mathematics.
 Python can be used for rapid prototyping, or
for production-ready software
development.
CONTD…
 Python works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc).
 Python has a simple syntax similar to the English
language.
 Python has syntax that allows developers to write
programs with fewer lines than some other programming
languages.
 Python runs on an interpreter system, meaning that
code can be executed as soon as it is written. This
means that prototyping can be very quick.
 Python can be treated in a procedural way, an object-
oriented way or a functional way.
FEATURES OF PYTHON
PROGRAMMING LANGUAGE
 Simple
 Easy to Learn
 Free and Open source
 High Level Language
 Interpreted
 Portable
 Object Oriented
 Extensible
 Embeddable
 Extensive Libraries
 Designed for readability, and has some similarities
to the English language with influence from
COMPARISON OF PYTHON WITH OTHER PROGRAMMING LANGUAGES

mathematics.

 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.
TYPES OF MODES TO RUN
PYTHON
 In the Python programming language, there are
two ways in which we can run our code:
 1. Interactive mode
 2. Script mode
INTERACTIVE MODE
In this mode lines of code are directly written
and executed in the Python interpreter shell,
which instantaneously provide the results.
NORMAL OR SCRIPT MODE
 In this mode the source code is saved to a
file with .py extension, and the file is
executed by the python interpreter.
FOR EXAMPLE:
 print("Hello World!")
PYTHON COMMENTS

 Comments can be placed at the end of a line

 Comments starts with a #, and Python will


ignore them

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


PYTHON VARIABLE
 In Python a variable is created the moment
you first assign a value to it.
 Example:
 x = 5
y = "John"
print(x)
print(y)
TWO TYPES OF ERRORS

 Syntax errors 
 Runtime errors
SYNTAX ERROR
 Syntax errors are like grammatical errors in
English. They occur when the defined rules are
not followed.
 For example:
 prin("Hello World")
 The Python interpreter generates the following
syntax error as it did not find a name prin (as
the correct method name is print).
RUNTIME ERROR
 Occur due to incorrect algorithm/logic or
wrong operations like dividing by zero during
the program execution.
EXAMPLE OF RUNTIME ERROR
EXAMPLE
 Write a Program to Print:-
 Python is Easy

 Solution:-
 print(“Python is Easy”)
Interesting ways to use
Print functions
1ST: WITH COMMA AS A
SEPARATOR
2ND: WITH SPACE AS A
SEPARATOR
3RD: WITH REPEAT CHARACTER
 We can print a string n times by using repeat
character (*) as shown below:
EXAMPLE:
TYPES OF COMMENTS IN
PYTHON
 1) Single-line Comment
 2) Docstring Comment
SINGLELINE COMMENT
 It starts with # (hash) and the content
following hash till the end of that line is a
comment
DOCSTRING COMMENT
 Python has a way to specify documentation
comments known as docstrings.
 A docstring is a simple text usually in English,
placed in your code to explain what a
specific part of code does.
 A docstring is a text enclosed in triple
quotes, and it’s usually the first non-
comment statement in a module, function,
class or method definition.
 This docstring becomes a special attribute
called __doc__ for that object.
 Python allows both triple double quote “””
and triple single quote’’’ to create docstring.
 The main purpose of docstring in python is
to provide information on what a particular
python does and not how It does.
 Docstring always begin with capital letter
and end with a period(.).
 There are two types of docstring:

 One line docstring


 Multiline docstring
 “””
 This is a single line comment.”””
 print(“Hello World”)

 “””
 This is a multiline comment.
 And I am printing hello world”””
 print(“Hello World”)
IDENTIFIER
 An Identifier is a name used to identify a
variable, function, class, module or object.

 In Python, identifier is like a noun in English.

 Identifier helps in differentiating one entity


from other.

 For example: name and age which speak of


two different aspects are called IDENTIFIERS.
 Python is a case-sensitive programming
language. Means, Age and age are two
different Identifiers.
RULES FOR WRITING IDENTIFIERS
 Identifier can be a combination of lowercase letters (a
to z) or uppercase letters (A-Z) or digits (0-9) or an
underscore (_).

 An Identifier can start with an alphabet or an


underscore(_), but not with a digit.

 Keywords can not be used as identifiers. Keywords are


reserved words in Python which have a special meaning.

 Few examples of Keywords: def, and, not, for, while, if,


else and so on.

 Special symbols are not allowed in Identifiers. Only one


special symbol Underscore (_) is allowed.
Examples of Python Identifiers

Valid identifiers:
var1
_var1
_1_var
var_1
Invalid Identifiers
!var1
1var
1_var
var#1
var 1
PYTHON KEYWORDS
 Python has a set of keywords that are
reserved words that cannot be used as
variable names, function names, or any
other identifiers.
 Python 3.5 has 33 keywords.
 Python 3.7 has 35 keywords.
Keyword Description

and A logical operator

as To create an alias

assert For debugging

break To break out of a loop

class To define a class

continue To continue to the next iteration of a loop

def To define a function

del To delete an object

elif Used in conditional statements, same as else if

else Used in conditional statements

except Used with exceptions, what to do when an exception occurs

False Boolean value, result of comparison operations

finally Used with exceptions, a block of code that will be executed no matter if there is an exception or not

for To create a for loop

from To import specific parts of a module

global To declare a global variable

if To make a conditional statement

import To import a module

in To check if a value is present in a list, tuple, etc.

is To test if two variables are equal

lambda To create an anonymous function

None Represents a null value

nonlocal To declare a non-local variable

not A logical operator

or A logical operator

pass A null statement, a statement that will do nothing

raise To raise an exception

return To exit a function and return a value

True Boolean value, result of comparison operations

try To make a try...except statement

while To create a while loop

with Used to simplify exception handling

yield To end a function, returns a generator


PRINT LIST OF KEYWORDS
Import keyword
Print(keyword.kwlist)
TO FIND KEYWORD
 import keyword
 print(keyword.iskeyword("and"))
VARIABLES AND ASSIGNMENT
 Variable is reserved memory location used to
store values.
 For example: age = 21
 For example: city = “Tokyo”

 Usually in C,C++ and Java, we need to


declare variables along with their types
before using them.
 Python being a dynamically typed language,
there is no need to declare variables or
declare their types before using them.
CONTD…
 Python has no command for declaring a variable.

 A variable is created the moment a value is


assigned to it.

 The equal-to(=) operator is used to assign value to


a variable.

 For example: marks = 100


 Here marks is the variable and 100 is the value
assigned to it.

 Note: As a good programming practice, one should


use meaningful names for variables.
CONTD…
 Python interpreter automatically determines
the type of the data, based on the data
assigned to it.
 In Python, we can even change type of the
variable after it has been set once(or
initialized with some other type).

 This can be done by assigning a value of a


different type to the variable.

 The most commonly used data types in Python


are: Numbers, List, Tuple, Strings, Dictionary
etc.
CONTD…
 Associating a value with a variable using the
assignment operator (=) is called as binding.

 In Python, assignment creates references, not copies.

 Python allows you to assign a single value to several


variables simultaneously.

 Example:
 number1 = number2 = number3 = 100

 Here, an integer object is created with the value 100,


and all the three variables are references to the same
memory location. This is called Chained Assignment.
MULTIPLE ASSIGNMENT
 Value1, value2, value3 = 7, 8.5, “Python”

 Here integer object 7 is assigned to value1


 Float object 8.5 is assigned to value2
 String object is assigned to value3
 Chained assignment is recognised and
supported as a special case of the assignment
statement.

 a= b=c=str is called a chained statement in


which value of str is assigned to multiple
variables.
VERIFY THE TYPE OF OBJECT
 x=1
 y = 35656222554887711
 z = -3255522

 print(type(x))
 print(type(y))
 print(type(z))
ADD A VARIABLE TO ANOTHER
 x = "Python "
 y = "is "
 z = "awesome"
 print(x + y + z)
OUTPUT BOTH TEXT AND
VARIABLES
 x = "Python"
 y = "is"
 z = "awesome"
 print(x, y, z)
TYPE CONVERSION
CASTING INTEGER
 x = int(1)
 y = int(2.8)
 z = int("3")
 print(x)
 print(y)
 print(z)
CASTING FLOAT
 x = float(1)
 y = float(2.8)
 z = float("3")
 w = float("4.2")
 print(x)
 print(y)
 print(z)
 print(w)
CASTING STRING
 x = str("s1")
 y = str(2)
 z = str(3.0)
 print(x)
 print(y)
 print(z)

You might also like