0% found this document useful (0 votes)
2 views12 pages

Python Day1

The document provides a comprehensive overview of Python programming, including its definitions, types, and features. It covers the installation process, environment configuration, and fundamental concepts such as identifiers, variables, keywords, and data types. Additionally, it discusses control statements, functions, and their parameters, along with examples and rules for writing Python code.
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)
2 views12 pages

Python Day1

The document provides a comprehensive overview of Python programming, including its definitions, types, and features. It covers the installation process, environment configuration, and fundamental concepts such as identifiers, variables, keywords, and data types. Additionally, it discusses control statements, functions, and their parameters, along with examples and rules for writing Python code.
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
You are on page 1/ 12

Python Programming

Computer Language
definitions
set of instructions (algorithm)
implementation of algorithm
helps us to interact with hardware
medium of communication with hardware
types
based on the level
low level
binary (0s and 1s)
middle level
interacts with CPU
Assembly language
opcodes: operation code => binary
e.g. ADD A, B
high level
developer can write human understable code
compiler or interprter convers the human understandable to machine (CPU)
understandable (ASM)
e.g. C++, Java, Python
based on how the application gets generated
compiled language
compile: converting human understable to machine (CPU) understandale
compiler: program which does compilation
executable:
program which contains only ASM instructions (machine understandable)
native applications
always platform (OS) dependent
faster than interpreted program
requires compiler
the entire program gets converted into executable
if program contains error, compiler detects these error at compilation time
e.g. C, C++
interpreted language
interpretation: which converts the human understandable to machine (CPU)
understandable line by line
interpreter: program which does interpretation
no executable gets generated
if there is any error, it will get detected at the run time
program will be always platform (OS) independent
programs will be always slower than native applications
e.g. html/CSS, JS, bash scripting
mixed language
shows behavior from both (compiled as well as interpreted)
uses compiler as well as interpreter
e.g. Java, Python
Introduction to Python
is high-level language which shows behavior from both compiled as well as interpreted languages
developed by Guido Rossum
can be used for
console application
web application
ML application
GUI application
the python by default imports the basic packages for using the built-in function
python application does NOT require any entry point function
python is one of the scripting languages
the code starts execution from from top (line 1) to bottom
python is a
scripting language
oop language
functional programming language
aspect oriented programming language
environment configuration
versions
1.x: deprecated
2.x: about to be deprecated
3.x: latest version
installation
to install python on ubuntu
sudo apt-get install python3 python3-pip
to install python on centos
sudo yum install python3 python3-pip
IDE
PyCharm
Community Edition
https://www.jetbrains.com/pycharm/download/
Spyder
Visual Studio Code
vim
configuration
fundamentals
identifier
valid word which is used to perform an action
most of the times the identifiers are lower cased
can be
variable name
function name
constant
class name
keyword
rules
can not start with number
e.g.
1name is invalid identifier
one_name is valid identifier
can not contain special character like space
e.g.
first name is invalid identifier
first_name is valid identifier
may use only underscore (_)
conventions
for variables: lower case
e.g. name, address, first_name
for functions: lower case with underscore
e.g. is_eligible_for_voting
for class: lower case with first letter uppercase
e.g. Person, Mobile
variable
identifier used to store a value
variable can not be declared explicitly
syntax
<variable name> = <initial value>
e.g.
num = 100
keyword
reserved identifier by python
can not use keyword for declaring variables or functions
e.g. if, elif, else, for, while, switch
pass
do not do anything
pass the control to the next line
used to create empty function/class
def
used to define a function
return
used to return a value
statement
the one which executes
unit of execution
semicolon ( ; ) is used to terminate a statement
one statement per line does not require semicolon ( ; )
BUT MULTIPLE STATEMENTS ON ONE LINE MUST USE SEMICOLON ( ; )
types
assignment statement
declaration statement
function call
control statement
conditional statement
comment
ignored while execution
to create comment use symbol #
RULE
if the code has any synctical error, the python compiler will not generate the byte codes [the
syntactical errors will be detected at the time compilation]

print("hello 1")
print("hello 2"

# this code will generate SyntaxError


# even the first line will NOT get executed

if the code has any run time error, the compilation will not detect any error and program will
execute till the line where the error is detected

print("hello 1")
printf("hello 2")

# this code will generate NameError


# the first line will get executed and code will stoop on the line 2

block
group of statements
use space(s)/tab(s) [indentation] to create a block
e.g. function, if, else, while
control statements
if..else
used to check a condition
e.g.

if p1 % 2 == 0:
print(f"{p1} is even")
else:
print(f"{p1} is not even")

data types
in python, all data types are inferred
in python, all data types are assigned implicitly
data types will get assigned automatically (by python itself) by looking at the CURRENT value of
the variable
you can not declare a variable with explicit data type
e.g.
# can not declare explicit
# int num = 100

types
int
represents the whole numbers (+ve or -ve)
e.g.
num = 100
myvar = -10
float
represents a value with decimal
e.g.
salary = 10.60
str
represents a string
to create a string value use
single quotes
used to create single line string
e.g.

name = 'steve'

double quotes
used to create single line string
e.g.

last_name = "jobs"

tripe double quotes


used to create multi-line string
e.g.

address = """
House no 100,
XYZ,
pune 411056,
MH, India.
"""

bool
represents boolean value
can contain one of the two values [True/False]
e.g.

can_vote = True

complex
object
operators
mathematical
+ : addition/string concatination
- : subtraction
* : multiplication
/ : true division (float)
//: floor division (int)
**: power of
comparison
== : equal to
!= : not equal
> : greater than
< : less than
>=: greater than or equal to
<=: less than or equal to
logical
and:
logical and operator
returns true only when both the conditions are true
rule
true and true => true
true and false => false
false and true => false
false and false => false

if (age > 20) and (age < 60):


print(f"{age} is within the limit")
else:
print(f"{age} is not within the limit")
or:
logical or operator
returns true when one of the conditions is true
rule
true or true => true
true or false => true
false or true => true
false or false => false

if (age > 20) or (age < 60):


print(f"{age} is within the limit")
else:
print(f"{age} is not within the limit")

function
named block
can be used to reuse the code
in python, function name is treated as a variable (the type of such variable is function)
function uses c calling conventions

def function_1():
pass

# type of function_1: function


print(f"type of function_1: {type(function_1)}")

scope
global
declared outside of any function
such variables can be accessed anywhere (outside or inside of any function) in the code (in
the same file)
by default global variables are not modifiable inside function(s)
use global keyword to make the global variables modifiable
e.g.

num = 100
# global variable
# num = 100
print(f"outside function num = {num}")

def function_1():
# num = 100
print(f"inside function_1, num = {num}")

def function_2():
# the num will refer to the global copy
global num

# modify the global variable


num = 200

local
variable declared inside a function
the variable will be accessible only within the function (in which, it is declared)
local variables will NOT be accessible outside the function
e.g.

def function_1():
num = 100
print(f"inside function_1, num = {num}")

function_1()

# the statement will generate NameError as


# num is a local variable
# print(f"outside function_1, num = {num}")

custom
also known as user defined function
e.g.

# function declaration
def function_1():
print("inside function_1")

# function invocation
function_1()

function parameters

positional parameters
do not have parameter name whlie making the function name
the values will get assigned to the parameters from left to right
the position of paramter is very important
e.g.

def function_3(num1, num2, num3):


print(f"num1 = {num1}, num2 = {num2}

# num1 = 10, num2 = 20, num3 = 30


function_3(10, 20, 30)

named parameters
the function call will contain the parameter name along with the parameter value
position of the named parameter is not important
e.g.

def function_3(num1, num2, num3):


print(f"num1 = {num1}, num2 = {num2}

# num1 = 10, num2 = 20, num3 = 30


function_3(num1=10, num2=20, num3=30)
function_3(num2=20, num3=30, num1=10)
function_3(num3=30, num2=20, num1=10)

optional parameters
a function can set a default value for a parameter
caller does not need to pass a value for such parameters
the parameter having default value becomes optional (caller may or may not pass the value
for such parameter)

# p2 has a default value = 50


def function_1(p1, p2=50):
print(f"{p1}, {2}")

# p1 = 10, p2 = 50
function_1(10)

# p1 = 10, p2 = 20
function_1(10, 20)

function types
empty function
function without a body

def empty_function():
pass

parameterless function
function which does not accept any parameter

def function_1():
print("inside function_1")

parameterized function
function which accepts at least one parameter
e.g.

def function_1(p1):
print(f"p1 = {p1}, type = {type(p1)}")

function_1(10) # p1 = int
function_1("10") # p1 = str
function_1(True) # p1 = bool

non-returning function
function which does not return any function
e.g.

def function_1():
print("inside function_1")

a non-returning function always will return None


e.g.

def function_1():
print("inside function_1")

result = function_1()

# result = None
print(f"result = {result}")

returning function
function which returns a value
e.g.

# returning function
def add(p1, p2):
print("inside add")
return p1 + p2

# capture result
addition = add(30, 50) # 80

nested function
function within a function
is also known as inner or local function
the inner function can be called only within the function in which it is declared
e.g.

def outer():
def inner():
pass

# inner is callable only within outer


inner()

outer()

# can not access inner outside the outer

You might also like