0% found this document useful (0 votes)
19 views

Lecture-04 (Python Practical Class-02)

The document discusses the difference between using '+' and ',' in the print function in Python. It shows that '+' concatenates all values into a string while ',' separates values with a space. It also covers string formatting, string methods, dealing with quotation marks, parameters and arguments in functions.

Uploaded by

Mahmudul hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lecture-04 (Python Practical Class-02)

The document discusses the difference between using '+' and ',' in the print function in Python. It shows that '+' concatenates all values into a string while ',' separates values with a space. It also covers string formatting, string methods, dealing with quotation marks, parameters and arguments in functions.

Uploaded by

Mahmudul hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Practical-02

The Difference between ‘+’ and ‘,’


➢ When you use + in the print function, you are
concatenating the values into a single string before it's
printed.

➢ For Example:

name = "John"
age = 30

print("Name: " + name + ", Age: " + str(age))


# Output: Name: John, Age: 30
2
The Difference between ‘+’ and ‘,’
➢ When you use the ‘,’ in the print function, it separates
the values by inserting a space between them. It also
automatically converts non-string values to strings, so
you don't need to use str() explicitly.

➢ For Example:

name = "John"
age = 30

print("Name:", name, "Age:", age)


# Output: Name: John Age: 30
3
Formatting Strings
➢ Probably the most elegant way to use strings would be
as follows:
name = input("What's your name? ")
print(f"hello, {name}")

➢ Notice the f in print(f"hello, {name}"). This f is a


special indicator to Python to treat this string a special
way, different than previous approaches.

4
String Methods
➢ You should never expect your user will cooperate as
intended. Therefore, you will need to ensure that the input of
your user is corrected or checked.
➢ It turns out that built into strings is the ability to use
different types of methods.
➢ Some popular methods are:
➢ strip()
➢ capitalize()
➢ title()
➢ lower()
➢ upper()
➢ replace()
➢ More can be found in:
5
https://docs.python.org/3/library/stdtypes.html#str
Comments

#
➢ Comments are a way for programmers to track what they are
doing in their programs and even inform others about their
intentions for a block of code.
➢ In short, they are notes for yourself and others that will see your
code.
➢ Pseudocode is an important type of comment that becomes a
special type of to-do list, especially when you don’t understand
how to accomplish a coding task.
➢ Shortcut in Pycharm: ctrl 0/

6
A Small Problem with Quotation Marks
➢ Notice how adding quotation marks as part of your
string is challenging.
➢ print("hello,"friend"") will not work and the compiler
will throw an error.
➢ Generally, there are two approaches to fixing this.
First, you could simply change the quotes to single
quote marks.
➢ Another, more commonly used approach would be
code as print("hello, \"friend\""). The backslashes tell
the compiler that the following character should be
considered a quotation mark in the string and avoid a
compiler error.
7
Parameters
➢ Parameters are the placeholders or variables defined in
the function signature that represent the data that a
function expects to receive when it is called.
➢ They act as local variables within the function, and
their values are provided when the function is called
with arguments.
➢ Parameters are defined when you create a function and
are used to specify the input the function expects to
work with.

8
Arguments
➢ Arguments are the actual values or expressions that
are passed to a function when it is called.
➢ When you call a function, you provide the arguments
to match the parameters defined in the function
signature.
➢ The order and number of arguments you pass during
the function call must match the order and number of
parameters defined in the function.

9
Parameters vs Arguments
➢ Parameters:

def add_numbers(a, b): # Here 'a' and 'b' are


parameters
return a + b

➢ Arguments
result = add_numbers(5, 10) # Here '5' and '10' are
arguments

10
Parameters: print() Function

print(*objects, sep=' ', end='\n', file=None, flush=False)

Check out the following website for all the built-in functions
of Python:
https://docs.python.org/3/library/functions.html

11
That’s All
For Today!
12

You might also like