Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Python for Programmers

You're reading from   Python for Programmers A Comprehensive Guide for Intermediate to Advanced Python Programmers and Developers

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Mercury_Learning
ISBN-13 9781836645030
Length 293 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Mercury Learning and Information Mercury Learning and Information
Author Profile Icon Mercury Learning and Information
Mercury Learning and Information
Oswald Campesato Oswald Campesato
Author Profile Icon Oswald Campesato
Oswald Campesato
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface
1. Chapter 1: Introduction to Python 2. Chapter 2: Conditional Logic in Python FREE CHAPTER 3. Chapter 3: Data Structures in Python 4. Chapter 4: Strings and Arrays 5. Chapter 5: Built-In Functions and Custom Classes 6. Chapter 6: Recursion and Combinatorics 7. Index
Appendix: Introduction to Pandas

QUOTATION AND COMMENTS IN PYTHON

Python allows single (‘), double (“) and triple (‘’’ or “””) quotes for string literals, provided that they match at the beginning and the end of the string. You can use triple quotes for strings that span multiple lines. The following examples are legal Python strings:

word = 'word'

line = "This is a sentence."

para = """This is a paragraph. This paragraph contains

more than one sentence."""

A string literal that begins with the letter “r” (for “raw”) treats everything as a literal character and “escapes” the meaning of meta characters, as shown here:

a1 = r'\n'

a2 = r'\r'

a3 = r'\t'

print('a1:',a1,'a2:',a2,'a3:',a3)

The output of the preceding code block is here:

a1: \n a2: \r a3: \t

You can embed a single quote in a pair of double quotes (and vice versa) in order to display a single quote or a double quote. Another way to accomplish the same result is to precede a single or double quote with a backslash (“\”) character. The following code block illustrates these techniques:

b1 = "'"

b2 = '"'

b3 = '\''

b4 = "\""

print('b1:',b1,'b2:',b2)

print('b3:',b3,'b4:',b4)

The output of the preceding code block is here:

b1: ' b2: "

b3: ' b4: "

A hash sign (#) that is not inside a string literal is the character that indicates the beginning of a comment. Moreover, all characters after the # and up to the physical line end are part of the comment (and ignored by the Python interpreter). Consider the following code block:

#!/usr/bin/python

# First comment

print("Hello, Python!")  # second comment

This will produce following result:

Hello, Python!

A comment may be on the same line after a statement or expression:

name = "Tom Jones" # This is also comment

You can comment multiple lines as follows:

# This is comment one

# This is comment two

# This is comment three

A blank line in Python is a line containing only whitespace, a comment, or both.

lock icon The rest of the chapter is locked
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Python for Programmers
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon