0% found this document useful (0 votes)
1 views4 pages

Advc Python

The document outlines the basics of advanced Python programming, focusing on print statements and variable creation in Module 1. It covers different techniques for using quotes in print statements, how to create and display variables, and introduces string data types and operations in Module 2. Key topics include string concatenation, indexing, and slicing, along with examples of manipulating strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Advc Python

The document outlines the basics of advanced Python programming, focusing on print statements and variable creation in Module 1. It covers different techniques for using quotes in print statements, how to create and display variables, and introduces string data types and operations in Module 2. Key topics include string concatenation, indexing, and slicing, along with examples of manipulating strings.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ADVANCE PYTHON PROGRAMMING

BASICS
Proitbridge Data Science & ML Program

MODULE 1
1.1 Different Techniques — print() Statements
1.2 Create a Variable

Rule 1 — Single Quotes


Use single quotes for a simple sentence (no apostrophes or inner quotes).

print('my name is viswanath')

Output:
my name is viswanath

Rule 2 — Double Quotes


Use double quotes when the string itself contains single quotes or apostrophes.

print("she said, 'I'm AI john's coach learner")

Output:
she said, 'I'm AI john's coach learner

Rule 3 — Triple Quotes


Use triple quotes (""" """) for long multi-line strings.

print("""Are you looking for instructions for a specific game?


Because there are thousands of games in the world—from classic card games
to modern board games—let me know which one you need help with!""")

Output:
Are you looking for instructions for a specific game? Because there are thousands
of games
in the world—from classic card games to modern board games—let me know which one
you need help with!

1.2 Create a Variable


A variable stores a value so you can reuse it later.

items_to_buy = ("Tomato", "Potato")


items_to_buy # Display the variable

Output:
('Tomato', 'Potato')

MODULE 2
Topics covered in this module:
1. What is a String?
2. String Concatenation, Indexing & Slicing
3. Manipulate Strings with String Methods
4. Interact with User Input
5. Challenge
6. Streamline the Print Statement

2.1 What is a String?


Anything enclosed in quotes is called a String.

Python Data Types:


• int — Numbers WITHOUT decimal points (e.g. 9)
• float — Numbers WITH decimal points (e.g. 9.5)
• str — Anything enclosed within quotes (e.g. "9.5")
• bool — True or False

Number inside quotes → str


Average_grade = "9.5" # String
Average_grade
type(Average_grade)

Output:
'9.5'
str

Number without quotes → float


Average_grade = 9.5 # Float
Average_grade
type(Average_grade)

Output:
9.5
float
Whole number without quotes → int
Average_grade = 9 # Integer
Average_grade
type(Average_grade)

Output:
9
int

2.2 String Concatenation, Indexing & Slicing

Concatenation — Joining Strings


Use + to join strings. An int must be converted to str() first.

firstname = "viswanath" # str


lastname = "gangaram" # str
age = 40 # int

Example 1 — Error when adding int directly:


print(firstname + lastname + 40)

Error:
TypeError: can only concatenate str (not "int") to str

Example 2 — Convert int to str() first:


print(firstname + lastname + str(40))

Output:
viswanathgangaram40

Example 3 — Add spaces and separators:


print(firstname + ' ' + lastname + '_' + str(40))

Output:
viswanath gangaram_40

Example 4 — Using commas (auto space):


print(firstname, lastname, '_', age)

Output:
viswanath gangaram _ 40

Example 5 — Custom separator with commas:


print(firstname, '_', lastname, '_', age)

Output:
viswanath _ gangaram _ 40
Indexing & Slicing
len() returns the total number of characters. Slicing syntax: string[start:end] — end index is NOT included.

len(firstname) # "viswanath" has 9 characters

Output:
9

firstname[0:4] # Characters at index 0,1,2,3 → "visw"

Output:
'visw'

2.3 Manipulate Strings with String Methods


(This section will be completed in the next class session.)

You might also like