Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
113 views

Introduction To Python (Part I)

This document provides an introduction to Python programming. It discusses why Python is an expressive, object-oriented language and how to get started. It covers installing Python and an IDE. Basic Python concepts like strings, escape sequences, printing, and functions are explained. The document provides examples to illustrate these concepts and solve problems like drawing different shapes using functions.

Uploaded by

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

Introduction To Python (Part I)

This document provides an introduction to Python programming. It discusses why Python is an expressive, object-oriented language and how to get started. It covers installing Python and an IDE. Basic Python concepts like strings, escape sequences, printing, and functions are explained. The document provides examples to illustrate these concepts and solve problems like drawing different shapes using functions.

Uploaded by

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

INTRODUCTION TO PYTHON

(PART I)

Presenter: Prof. Amit Kumar Das


Assistant Professor,
Dept. of Computer Science and Engg.,
Institute of Engineering & Management.
WHY PYTHON?

-- Expressive language
 expresses complex ideas in a simple way
 well-designed

-- Object-oriented

-- Pre-written software

-- Widely used especially for solving problems using


machine learning tools and techniques
GETTING STARTED
First install Python. Then install an IDE (or integrated development
environment) for Python – IDLE being the default IDE which has been
bundled with the default implementation of Python since 1.5.2b1. Another
option is to install Anaconda*, which is a scientific Python
distribution. The default IDE bundled with Anaconda is Spyder.

After the IDE is installed, install basic libraries of Python. To start


with, install the library “os” for using operating system dependent
functions
Command: pip install
Syntax: pip install 'SomePackage‘
After installing, libraries need to be loaded by invoking the command:
Command: import <<library-name>>
Syntax: import os

* There is a minimal Anaconda Python without all the packages, called Miniconda.
After installing miniconda, you can use conda and install only those scientific
packages that you wish and avoid a bloated installation.
A PYTHON PROGRAM
Code comment

Lines of Code

Console

Output
COMMAND: PRINT( )

Used to print a line of output on the


console

Three ways to use print :


• print("…string…")
Prints the given string as output

• print(var)
Prints a variable value as output

• print()
Prints a blank line of output
STRING
string: A sequence of characters
-- Starts and ends with a " quote " character or a '
quote ' character
-- The quotes do not appear in the output when printed

Examples:

"hello"
"This is a string. It's very long!"
'Here is "another" with quotes in’
STRING (CONTD.)
Syntax Rules:
Strings surrounded by " " or ' ' may not span
multiple lines

"This is not
a legal String."

Strings surrounded by " " may not contain a "


character.
"This is not a "legal" String either."

Strings surrounded by ' ' may not contain a '


character.
'This is not a 'legal' String either.’

So, how to handle these problems?


ESCAPE SEQUENCES

A sequence of characters used to represent certain


special characters in a string.

\t tab character
\n new line character
\" quotation mark character
\\ backslash character

“Now, this is \n a legal String."

"This is also a \"legal\" String."

'This is another \'legal\' String.’


OPERATIONS ON STRINGS

String concatenation: + operator


print("Hello," + " world!")

Use + str(value) to print a string and a


variable's value on one line.
>>> marks = (95.1 + 71.9 + 82.6) / 3.0
>>> print("Your marks is " + str(marks))

apples = 13 + 17 + 5
print("There are " + str(apples) + " apples in the
basket.")
Output:
Your grade was 83.2
There are 35 apples in the basket.
LET’S TAKE A PAUSE: QUIZ TIME …
>>> What print statements will generate this output?
A "quoted" String is
'much' better if you learn
the rules of "escape sequences."
Also, "" represents an empty String.
Don't forget: use \" instead of " !
'' is not the same as “
>>> Write a program to print the following figures.
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
LET’S LOOK AT THE SOLUTIONS
>>> print("A \"quoted\" String is")
>>> print("'much' better if you learn")
>>> print("the rules of \"escape sequences.\"")
>>> print()
>>> print("Also, \"\" represents an empty String.")
>>> print("Don't forget: use \\\" instead of \" !")
>>> print("'' is not the same as \"")

OR

>>> print("A \"quoted\" String is \n 'much' better


if you learn \n the rules of \"escape sequences.\"")
>>> print("\n Also, \"\" represents an empty String.
\n Don't forget: use \\\" instead of \" !\n '' is
not the same as \"")
LET’S LOOK AT THE SOLUTIONS (CONTD.)
>>> print(" ______")
>>> print(" / \\")
>>> print("/ \\")
>>> print("\\ /")
>>> print(" \\______/")
>>> print()
>>> print("\\ /")
>>> print(" \\______/")
>>> print("+--------+")
>>> print()
CAN WE DO IT
>>> print(" ______")
>>> print(" / \\") IN A BETTER WAY!!!
>>> print("/ \\")
>>> print("| STOP |")
>>> print("\\ /")
>>> print(" \\______/")
>>> print()
>>> print(" ______")
>>> print(" / \\")
>>> print("/ \\")
>>> print("+--------+")
WHAT IS ALGORITHM?
A list of steps for solving a problem

Example : Bake cookies


 Mix the dry ingredients.
 Cream the butter and sugar.
 Beat in the eggs.
 Stir in the dry ingredients.
 Set the oven temperature.
 Set the timer for 10 minutes.
 Place the cookies into the oven.
 Allow the cookies to bake.
 Mix ingredients for frosting.
 Spread frosting and sprinkles onto the cookies.
WHAT ARE THE PROBLEMS?

 Lack of structure: Many steps – difficult to


understand the flow and logic at a glance

 May need repetition: Same set of steps to be


listed more than once

So how to solve these issues?


WHAT IS ALGORITHM?
Make the algorithm structured : Decompose it into
related tasks
1 - Make the batter
 Mix the dry ingredients.
 Cream the butter and sugar.
 Beat in the eggs.
 Stir in the dry ingredients.
2 - Bake the cookies
 Set the oven temperature.
 Set the timer for 10 minutes. Repeat these
 Place the cookies into the oven. steps, if needed

 Allow the cookies to bake.


3 - Decorate the cookies
 Mix ingredients for frosting.
 Spread frosting and sprinkles onto the cookies.
FUNCTION
A named sequence of statements
 supports the creation of well-structured programs
 eliminates repetition by code reuse

Syntax:
Space is part of the syntax.
def name():
statement Statements in a function
statement must have the same level of
... indentation.
Statement

Function names: May consist of upper and lower case


letters, "_", and digits 0 through 9.

Example:
def go_KKR():
print(“KKR will be the champs this IPL”)
print(“KKR .. Hai Taiyaar...”)
DECLARING AND CALLING FUNCTIONS
def message1():
print("This is message1.")

def message2():
print("This is message2.")
message1()
print("Done with message2.")
def main():
message1()
message2()
print("All done.")

Output:
This is message1.
This is message2.
This is message1.
Done with message2.
All done.
WHEN TO USE FUNCTIONS

• Consider creating functions when


 There are statements which are related
structurally
 There are statements which are repeated

• You should not create functions for


 An individual print statement
 Unrelated or weakly related statements
(Consider splitting them into two smaller
functions)
COMING BACK TO THE DRAWING PROBLEM …
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+

Should try to write a second version of the program which


is structured, without repetition. For that, we have to
• Identify the structure of the output
• Divide the code into functions based on this structure
LET’S LOOK AT THE SOLUTION (CONTD.)
The structure of the output:
 initial "egg" figure
______
 second "teacup" figure / \
/ \
 third "stop sign" figure \ /
\______/
 fourth "hat" figure
\ /
\______/
+--------+
This structure can be represented by
______
functions: / \
/ \
 egg | STOP |
\ /
 tea_cup \______/
 stop_sign ______
/ \
 hat / \
+--------+
LET’S LOOK AT THE SOLUTION (CONTD.)
def egg(): def stop_sign():
print(" ______")
print(" ______") print(" / \\")
print(" / \\") print("/ \\")
print("/ \\") print("| STOP |")
print("\\ /")
print("\\ /")
print(" \\______/")
print(" \\______/") print()
print()
def hat():
print(" ______")
def tea_cup(): print(" / \\")
print("\\ /") print("/ \\")
print(" \\______/") print("+--------+")
print("+--------+")
print()

def main():
egg()
tea_cup()
stop_sign()
hat()
LET’S LOOK AT THE SOLUTION (CONTD.)

The redundancy in the output:

• egg top: reused on stop sign, hat


• egg bottom: reused on teacup, stop sign
• divider line: used on teacup, hat

This redundancy can be fixed by functions:


• egg_top
• egg_bottom
• line
LET’S LOOK AT THE SOLUTION (CONTD.)
# Draws the top half of an an egg figure. # Draws a line of dashes.
def egg_top(): def line():
print("+--------+")
print(" ______")
print(" / \\") # Draws a teacup figure.
print("/ \\") def tea_cup():
egg_bottom()
line()
# Draws the bottom half of an egg figure. print()
def egg_bottom():
# Draws a stop sign figure.
print("\\ /")
def stop_sign():
print(" \\______/") egg_top()
print("| STOP |")
egg_bottom()
# Draws a complete egg figure.
print()
def egg():
egg_top() # Draws a figure that looks sort
of like a hat.
egg_bottom()
def hat():
print() egg_top()
line()
def main():
egg()
tea_cup()
stop_sign()
hat()
BASIC PYTHON COMMANDS
THANK YOU &
STAY TUNED!

You might also like