From a0361d051995f13d720315b8ad0a4b6623b40e66 Mon Sep 17 00:00:00 2001 From: ankitsaxena21 Date: Tue, 8 Oct 2019 02:39:15 -0700 Subject: [PATCH 1/2] added docstrings.py and modifies readme --- 3 - Comments/README.md | 9 +++++++++ 3 - Comments/comments_are_not_executed.py | 7 +++++++ 3 - Comments/docstrings.py | 10 ++++++++++ 3 files changed, 26 insertions(+) create mode 100644 3 - Comments/docstrings.py diff --git a/3 - Comments/README.md b/3 - Comments/README.md index 09b7b003..633361a7 100644 --- a/3 - Comments/README.md +++ b/3 - Comments/README.md @@ -4,3 +4,12 @@ Comments start with a hash character (#) and allow you to document your code. Comments are ignored when code is executed. - [Comments](https://docs.python.org/3/reference/lexical_analysis.html?highlight=comment) + +“”” Docstrings ””” + +The docstrings are declared using “””triple double quotes”””. +It gives programmers an easy way of adding quick notes with every Python module, function, class, and method. +Note: The strings defined using triple-quotation mark are docstring in Python. However, it might appear to you as a regular comment. +As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment. + +- [Docstrings](https://www.python.org/dev/peps/pep-0257/) diff --git a/3 - Comments/comments_are_not_executed.py b/3 - Comments/comments_are_not_executed.py index b3eb4553..a69612b9 100644 --- a/3 - Comments/comments_are_not_executed.py +++ b/3 - Comments/comments_are_not_executed.py @@ -2,3 +2,10 @@ # print('Hello world') # print("Hello world") # No output will be displayed! +""" + This is a docstring + written in + more than just one line. + It can be used as a multiline comment. +""" + diff --git a/3 - Comments/docstrings.py b/3 - Comments/docstrings.py new file mode 100644 index 00000000..c3b8dd56 --- /dev/null +++ b/3 - Comments/docstrings.py @@ -0,0 +1,10 @@ +def my_function(): + """Do nothing, but document it. + + This is a function docstring. + """ + + pass +#In order to print or access the docstrings use __doc__. +print(my_function.__doc__) + From 10b506c03bdbb0e4a95136613a0a65534a0d76bc Mon Sep 17 00:00:00 2001 From: ankitsaxena21 Date: Tue, 8 Oct 2019 03:43:47 -0700 Subject: [PATCH 2/2] Correct the code in format_strings.py and modified readme. --- 4 - String variables/README.md | 1 + 4 - String variables/format_strings.py | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/4 - String variables/README.md b/4 - String variables/README.md index a435dfbe..9cfa124e 100644 --- a/4 - String variables/README.md +++ b/4 - String variables/README.md @@ -3,6 +3,7 @@ Python can store and manipulate strings. Strings can be enclosed in single or double quotes. There are a number of string methods you can use to manipulate and work with strings - [strings](https://docs.python.org/3/tutorial/introduction.html#strings) +- [format strings](https://docs.python.org/3.4/library/string.html#string-formatting) - [string methods](https://docs.python.org/3/library/stdtypes.html#string-methods) Converting to string values diff --git a/4 - String variables/format_strings.py b/4 - String variables/format_strings.py index 6b795886..201869df 100644 --- a/4 - String variables/format_strings.py +++ b/4 - String variables/format_strings.py @@ -2,7 +2,18 @@ first_name = input('What is your first name? ') last_name = input('What is your last name? ') -# the capitalize function will return the string with -# the first letter uppercase and the rest of the word lowercase -print ('Hello ' + first_name.capitalize() + ' ' \ - + last_name.capitalize()) +# Custom string formatting +# Using the + operator to concatenate strings +print ('Hello ' + first_name + ' ' + last_name) + +# Using place holders to print strings +print ('Hello, {} {} '.format(first_name, last_name) + +# Using index numbers {0} and {1} to be sure the arguments are placed in the correct placeholders +print ('Hello {0} {1}'.format(first_name, last_name) + +# Reversing the index numbers to change the positions of placeholders +print ('Hello {1} {0}'.format(first_name, last_name) + +# Only available in Python 3 +print (f'Hello, {first_name} {last_name}')