Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Python #1 Let’s get started

Python: MOST POPULAR


PROGRAMMING
EVOLVED BY
COMMUNITY OF
LIBRARIES CAN BE
USED TO SOLVE
USED FOR
STIMULATIONS,

One
LANGUAGE TODAY DEVELOPERS PROBLEMS. PROTOTYPING
AND REAL-WORLD
APPLICATIONS

Language,
Many Uses SUPPORTS SUPPORTS APPS, ONE LANGUAGE,
WINDOWS, EV3, ARDUINO, MULTIPURPOSE
LINUX, MACOS, RASPBERRYPI AND USE
UBUNTU OTHER
ARCHITECTURE MICROCONTROLL
ERS.
Syntax similar to English language

Less Code

Python: • 1/10th of code compared to Java


• 1/100th of code compared to C++

Better than Lot’s of things Python does itself without explicit


other writing:
• Understands data types of the objects

languages • Smart Memory management: allocation and deallocation

Works with legacy code of other languages.

Interpreted at runtime
Python: More reasons to learn
Quiz
Which popular products are build using Python?
Let’s learn how
to print on
console
Print Syntax
print(message)
" "are used
because we
are printing
a string
Brackets are required message (characters)
because print is a
function
Print Text
• print("Sarvjeet Herald")  Print your name
• Text should be wrapped in Double quotes (")in the print function.

Would Single Quotes (') quotes work in print function?


Print Quotes
print( "Clark's shoes are good")

Will double quotes work in this context?


print(""Clarks" is a British based international shoe manufacturer")

What is the error?


SyntaxError: invalid syntax

Can you find early indication of error in the editor (before running code)?
Syntax Error
• Incorrect Python Grammar
• Code will not execute
• Tell about error in which line.

• Easy to resolve, just to the line number and fix the issue
Print Quotes
Wrong Way!
print(""Clarks" is a British based international shoe manufacturer")

Correct Way!
Use backslash escapes for printing single quotes in text
print("\"Clarks\" is a British based international shoe manufacturer")

OR
Replace outer double quotes with single quotes
print('"Clarks" is a British based international shoe manufacturer')
Print Quotes

Will single quotes work in this context?


print('Clark's shoes are good')

Is there any indication of error in the editor (before running code)?

Think, how can you fix this error.


Hint: Use backslash escape
Can you summarize learning from quotes

Double quotes should be wrapped by single quotes


Single Quotes should be wrapped by double quotes

Better Way!
Use of backslash escape makes life more easier
It is also compatible with legacy code
Exercises
• PRINT("I love food")
• Print("I love food")
• Print 6
• Print your address
• Print your height
• Print your phone number without zero
• Print your phone number starting with zero (e.g., 09958590959)
• What is the error?
• SyntaxError: invalid token
• Any idea, why this error?
• Leading zero means you are writing an octal notation and you can't have 9 or 8 in an octal
number.
Print two or more pieces of text
Print your first name and last name on a single line
print("Sarvjeet Herald")

But there is a better way


print("Sarvjeet", "Herald")
Did you notice? Values are neatly separated by space.

Why is it better?
Databases are Atomic. Indivisible and irreducible token to avoid partial updates.
Makes Create, Read, Update, Delete and Search Faster.
Exercise
• Print in a single line, your:
• First Name
• Last Name
• Age

• Extend this to include your mobile number on the same line


Different ways to separate text values
The values are separated by keyword argument sep

What do you think is the default value of the separator?


print(value, sep=" ")

Make result of print("Sarvjeet", "Herald",21.0) comma separated


print("Sarvjeet", "Herald",21.0, sep=",")

Print Descriptor “First Name” and Value on a single line separated by colon
print("First Name", "Herald",21.0, sep=":")
Useful when fetching and printing values from database
Different ways to separate text values
Print your first name, last name and age on different lines
print("sarvjeet")
print("herald")
print("21")

But there is a better way.


print("Sarvjeet", "Herald",21.0, sep="\n")
\n is the escape character used to print value in a new line
Different ways to end print output
Print statements ends by a keyword argument end

What do you think is the default value of the end?


print(value, sep=" ", end="\n")

In spite of using two different print statements as below,


can you make sure both are printed in a single line
print("sarvjeet")
print("herald")
Correct syntax is:
print("sarvjeet", end=" ")
print("herald")
Summarize different ways to modify print
output
• Print function can contain any number of values. These values can be text, number or
a number with decimal places.

• Separator keyword argument can describe how the values should be separated on a
single line e.g., comma separated, colon separated, etc
• Keyword is sep
• Default value of sep is Space

• End keyword argument can describe how the print function should be terminated.
• Keyword is end
• Default value is \n

You might also like