Introduction To Python (Part I)
Introduction To Python (Part I)
(PART I)
-- Expressive language
expresses complex ideas in a simple way
well-designed
-- Object-oriented
-- Pre-written software
* 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( )
• 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."
\t tab character
\n new line character
\" quotation mark character
\\ backslash character
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
Syntax:
Space is part of the syntax.
def name():
statement Statements in a function
statement must have the same level of
... indentation.
Statement
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
def main():
egg()
tea_cup()
stop_sign()
hat()
LET’S LOOK AT THE SOLUTION (CONTD.)