0% found this document useful (0 votes)
171 views137 pages

Python Code Examples - Sample Script Coding Tutorial For Beginners

Donate This article provides a thorough tutorial on Python syntax and code examples for beginners. It covers topics like variable definitions, data types, operators, conditionals, loops, functions, object-oriented programming, files, and more. Each concept is explained with descriptive text and illustrated with sample code. The goal is to guide new Python learners through coding concepts and help them on their programming journey.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
171 views137 pages

Python Code Examples - Sample Script Coding Tutorial For Beginners

Donate This article provides a thorough tutorial on Python syntax and code examples for beginners. It covers topics like variable definitions, data types, operators, conditionals, loops, functions, object-oriented programming, files, and more. Each concept is explained with descriptive text and illustrated with sample code. The goal is to guide new Python learners through coding concepts and help them on their programming journey.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 137

5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum

APRIL 27, 2021 / #PYTHON

Python Code Examples –


Sample Script Coding Tutorial
for Beginners
Estefania Cassingena Navone

Hi! Welcome. If you are learning Python, then this


article is for you. You will nd a thorough description of
Python syntax and lots of code examples to guide you
during your coding journey.

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 1/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
What we will cover:
Learn to code — free 3,000-hour curriculum
Variable De nitions in Python

Hello, World! Program in Python

Data Types and Built-in Data Structures in Python

Python Operators

Conditionals in Python

For Loops in Python

While Loops in Python

Nested Loops in Python

Functions in Python

Recursion in Python

Exception Handling in Python

Object-Oriented Programming in Python

How to Work with Files in Python

Import Statements in Python

List and Dictionary Comprehension in Python

and more...

Are you ready? Let's begin! 🔅

💡 Tip: throughout this article, I will use <> to indicate that this part
of the syntax will be replaced by the element described by the text.
For example, <var> means that this will be replaced by a variable
when we write the code.

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 2/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

🔹 Variable Definitions in Python Forum


The most basic building-block of any programming language is the
Donate

Learn to code — free 3,000-hour curriculum


concept of a variable, a name and place in memory that we reserve for
a value.

In Python, we use this syntax to create a variable and assign a value to


this variable:

<var_name> = <value>

For example:

age = 56

name = "Nora"

color = "Blue"

grades = [67, 100, 87, 56]

If the name of a variable has more than one word, then the Style Guide
for Python Code recommends separating words with an underscore
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 3/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

"as necessary to improve readability." Forum Donate

For example: Learn to code — free 3,000-hour curriculum

my_list = [1, 2, 3, 4, 5]

💡 Tip: The Style Guide for Python Code (PEP 8) has great suggestions
that you should follow to write clean Python code.

🔸 Hello, World! Program in


Python
Before we start diving into the data types and data structures that
you can use in Python, let's see how you can write your rst Python
program.

You just need to call the print() function and write "Hello, Worl
d!" within parentheses:

print("Hello, World!")

You will see this message after running the program:

"Hello, World!"

💡 Tip: Writing a "Hello, World!" program is a tradition in the


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 4/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

developer community. Most developers start learning how to code byDonate


Forum
writing this program.
Learn to code — free 3,000-hour curriculum
Great. You just wrote your rst Python program. Now let's start
learning about the data types and built-in data structures that you can
use in Python.

🔹 Data Types and Built-in Data


Structures in Python
We have several basic data types and built-in data structures that we
can work with in our programs. Each one has its own particular
applications. Let's see them in detail.

Numeric Data Types in Python: Integers,


Floats, and Complex
These are the numeric types that you can work with in Python:

Integers
Integers are numbers without decimals. You can check if a number is
an integer with the type() function. If the output is <class 'int'> ,
then the number is an integer.

For example:

>>> type(1)
<class 'int'>

>>> type(15)
<class 'int'>

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 5/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> type(0)
<class 'int'>
Forum Donate

Learn to code — free 3,000-hour curriculum


>>> type(-46)
<class 'int'>

Floats
Floats are numbers with decimals. You can detect them visually by
locating the decimal point. If we call type() to check the data type of
these values, we will see this as the output:

<class 'float'>

Here we have some examples:

>>> type(4.5)
<class 'float'>

>>> type(5.8)
<class 'float'>

>>> type(2342423424.3)
<class 'float'>

>>> type(4.0)
<class 'float'>

>>> type(0.0)
<class 'float'>

>>> type(-23.5)
<class 'float'>

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 6/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Complex Learn to code — free 3,000-hour curriculum
Complex numbers have a real part and an imaginary part denoted
with j . You can create complex numbers in Python with complex() .
The rst argument will be the real part and the second argument will
be the imaginary part.

These are some examples:

>>> complex(4, 5)
(4+5j)

>>> complex(6, 8)
(6+8j)

>>> complex(3.4, 3.4)


(3.4+3.4j)

>>> complex(0, 0)
0j

>>> complex(5)
(5+0j)

>>> complex(0, 4)
4j

Strings in Python
Strings incredibly helpful in Python. They contain a sequence of
characters and they are usually used to represent text in the code.

For example:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 7/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
"Hello, World!"
Learn to code — free 3,000-hour curriculum

'Hello, World!'

We can use both single quotes '' or double quotes "" to de ne a


string. They are both valid and equivalent, but you should choose one
of them and use it consistently throughout the program.

💡 Tip: Yes! You used a string when you wrote the "Hello, World!"
program. Whenever you see a value surrounded by single or double
quotes in Python, that is a string.

Strings can contain any character that we can type in our keyboard,
including numbers, symbols, and other special characters.

For example:

"45678"

"my_email@email.com"

"#IlovePython"

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 8/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
💡 Tip: Spaces are also counted as characters in a string.
Learn to code — free 3,000-hour curriculum

Quotes Within Strings


If we de ne a string with double quotes "" , then we can use single
quotes within the string. For example:

"I'm 20 years old"

If we de ne a string with single quotes '' , then we can use double


quotes within the string. For example:

'My favorite book is "Sense and Sensibility"'

String Indexing
We can use indices to access the characters of a string in our Python
program. An index is an integer that represents a speci c position in
the string. They are associated to the character at that position.

This is a diagram of the string "Hello" :

String: H e l l o
Index: 0 1 2 3 4

💡 Tip: Indices start from 0 and they are incremented by 1 for each
character to the right.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 9/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
For example:
Learn to code — free 3,000-hour curriculum

>>> my_string = "Hello"

>>> my_string[0]
'H'

>>> my_string[1]
'e'

>>> my_string[2]
'l'

>>> my_string[3]
'l'

>>> my_string[4]
'o'

We can also use negative indices to access these characters:

>>> my_string = "Hello"

>>> my_string[-1]
'o'

>>> my_string[-2]
'l'

>>> my_string[-3]
'l'

>>> my_string[-4]
'e'

>>> my_string[-5]
'H'

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 10/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


💡 Tip: we commonly use -1 to access the last character of a string.

String Slicing
We may also need to get a slice of a string or a subset of its characters.
We can do so with string slicing.

This is the general syntax:

<string_variable>[start:stop:step]

start is the index of the rst character that will be included in the
slice. By default, it's 0 .

stop is the index of the last character in the slice (this


character will not be included). By default, it is the last
character in the string (if we omit this value, the last character
will also be included).

step is how much we are going to add to the current index to


reach the next index.

We can specify two parameters to use the default value of step ,


which is 1 . This will include all the characters between start and st
op (not inclusive):

<string_variable>[start:stop]

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 11/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
For example:
Learn to code — free 3,000-hour curriculum

>>> freecodecamp = "freeCodeCamp"

>>> freecodecamp[2:8]
'eeCode'

>>> freecodecamp[0:3]
'fre'

>>> freecodecamp[0:4]
'free'

>>> freecodecamp[4:7]
'Cod'

>>> freecodecamp[4:8]
'Code'

>>> freecodecamp[8:11]
'Cam'

>>> freecodecamp[8:12]
'Camp'

>>> freecodecamp[8:13]
'Camp'

💡 Tip: Notice that if the value of a parameter goes beyond the valid
range of indices, the slice will still be presented. This is how the
creators of Python implemented this feature of string slicing.

If we customize the step , we will "jump" from one index to the next
according to this value.

For example:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 12/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn
>>> freecodecamp to code — free 3,000-hour curriculum
= "freeCodeCamp"

>>> freecodecamp[0:9:2]
'feCdC'

>>> freecodecamp[2:10:3]
'eoC'

>>> freecodecamp[1:12:4]
'roa'

>>> freecodecamp[4:8:2]
'Cd'

>>> freecodecamp[3:9:2]
'eoe'

>>> freecodecamp[1:10:5]
'rd'

We can also use a negative step to go from right to left:

>>> freecodecamp = "freeCodeCamp"

>>> freecodecamp[10:2:-1]
'maCedoCe'

>>> freecodecamp[11:4:-2]
'paeo'

>>> freecodecamp[5:2:-4]
'o'

And we can omit a parameter to use its default value. We just have to
include the corresponding colon ( : ) if we omit start , stop , or both:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 13/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn
>>> freecodecamp to code — free 3,000-hour curriculum
= "freeCodeCamp"

# Default start and step


>>> freecodecamp[:8]
'freeCode'

# Default end and step


>>> freecodecamp[4:]
'CodeCamp'

# Default start
>>> freecodecamp[:8:2]
'feCd'

# Default stop
>>> freecodecamp[4::3]
'Cem'

# Default start and stop


>>> freecodecamp[::-2]
'paeoer'

# Default start and stop


>>> freecodecamp[::-1]
'pmaCedoCeerf'

💡 Tip: The last example is one of the most common ways to reverse a
string.

f-Strings
In Python 3.6 and more recent versions, we can use a type of string
called f-string that helps us format our strings much more easily.

To de ne an f-string, we just add an f before the single or double


quotes. Then, within the string, we surround the variables or
expressions with curly braces {} . This replaces their value in the
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 14/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

string when we run the program. Forum Donate

Learn to code — free 3,000-hour curriculum


For example:

first_name = "Nora"
favorite_language = "Python"

print(f"Hi, I'm {first_name}. I'm learning {favorite_language}.")

The output is:

Hi, I'm Nora. I'm learning Python.

Here we have an example where we calculate the value of an


expression and replace the result in the string:

value = 5

print(f"{value} multiplied by 2 is: {value * 2}")

The values are replaced in the output:

5 multiplied by 2 is: 10

We can also call methods within the curly braces and the value
returned will be replaced in the string when we run the program:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 15/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


freecodecamp = "FREECODECAMP"

print(f"{freecodecamp.lower()}")

The output is:

freecodecamp

String Methods
Strings have methods, which represent common functionality that has
been implemented by Python developers, so we can use it in our
programs directly. They are very helpful to perform common
operations.

This is the general syntax to call a string method:

<string_variable>.<method_name>(<arguments>)

For example:

>>> freecodecamp = "freeCodeCamp"

>>> freecodecamp.capitalize()
'Freecodecamp'

>>> freecodecamp.count("C")
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 16/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> eecodeca p.cou t( C )
2 Forum Donate

Learn to code — free 3,000-hour curriculum


>>> freecodecamp.find("e")
2

>>> freecodecamp.index("p")
11

>>> freecodecamp.isalnum()
True

>>> freecodecamp.isalpha()
True

>>> freecodecamp.isdecimal()
False

>>> freecodecamp.isdigit()
False

>>> freecodecamp.isidentifier()
True

>>> freecodecamp.islower()
False

>>> freecodecamp.isnumeric()
False

>>> freecodecamp.isprintable()
True

>>> freecodecamp.isspace()
False

>>> freecodecamp.istitle()
False

>>> freecodecamp.isupper()
False

>>> freecodecamp.lower()
'freecodecamp'

>>> freecodecamp.lstrip("f")
'reeCodeCamp'
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 17/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
>>> freecodecamp.rstrip("p")
'freeCodeCam' Learn to code — free 3,000-hour curriculum

>>> freecodecamp.replace("e", "a")


'fraaCodaCamp'

>>> freecodecamp.split("C")
['free', 'ode', 'amp']

>>> freecodecamp.swapcase()
'FREEcODEcAMP'

>>> freecodecamp.title()
'Freecodecamp'

>>> freecodecamp.upper()
'FREECODECAMP'

To learn more about Python methods, I would recommend reading


this article from the Python documentation.

💡 Tip: All string methods return copies of the string. They do not
modify the string because strings are immutable in Python.

Booleans in Python
Boolean values are True and False in Python. They must start with
an uppercase letter to be recognized as a boolean value.

For example:

>>> type(True)
<class 'bool'>

>>> type(False)
<class 'bool'>

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 18/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


If we write them in lowercase, we will get an error:

>>> type(true)
Traceback (most recent call last):
File "<pyshell#92>", line 1, in <module>
type(true)
NameError: name 'true' is not defined

>>> type(false)
Traceback (most recent call last):
File "<pyshell#93>", line 1, in <module>
type(false)
NameError: name 'false' is not defined

Lists in Python
Now that we've covered the basic data types in Python, let's start
covering the built-in data structures. First, we have lists.

To de ne a list, we use square brackets [] with the elements


separated by a comma.

💡 Tip: It's recommended to add a space after each comma to make


the code more readable.

For example, here we have examples of lists:

[1, 2, 3, 4, 5]

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 19/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

["a", "b", "c", "d"] Forum Donate

Learn to code — free 3,000-hour curriculum

[3.4, 2.4, 2.6, 3.5]

Lists can contain values of different data types, so this would be a


valid list in Python:

[1, "Emily", 3.4]

We can also assign a list to a variable:

my_list = [1, 2, 3, 4, 5]

letters = ["a", "b", "c", "d"]

Nested Lists
Lists can contain values of any data type, even other lists. These inner
lists are called nested lists.

[[1, 2, 3], [4, 5, 6]]

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 20/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
In this example, [1, 2, 3] and [4, 5, 6] are nested lists.
Learn to code — free 3,000-hour curriculum
Here we have other valid examples:

[["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]

[1, [2, 3, 4], [5, 6, 7], 3.4]

We can access the nested lists using their corresponding index:

>>> my_list = [[1, 2, 3], [4, 5, 6]]

>>> my_list[0]
[1, 2, 3]

>>> my_list[1]
[4, 5, 6]

Nested lists could be used to represent, for example, the structure of a


simple 2D game board where each number could represent a different
element or tile:

# Sample Board where:


# 0 = Empty tile
# 1 = Coin
# 2 = Enemy
# 3 = Goal
board = [[0, 0, 1],
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 21/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
[0, 2, 0],
Learn
[1, 0, 3]] to code — free 3,000-hour curriculum

List Length
We can use the len() function to get the length of a list (the number
of elements it contains).

For example:

>>> my_list = [1, 2, 3, 4]

>>> len(my_list)
4

Update a Value in a List


We can update the value at a particular index with this syntax:

<list_variable>[<index>] = <value>

For example:

>>> letters = ["a", "b", "c", "d"]

>>> letters[0] = "z"

>>> letters
['z', 'b', 'c', 'd']
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 22/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum

Add a Value to a List


We can add a new value to the end of a list with the .append()
method.

For example:

>>> my_list = [1, 2, 3, 4]

>>> my_list.append(5)

>>> my_list
[1, 2, 3, 4, 5]

Remove a Value from a List


We can remove a value from a list with the .remove() method.

For example:

>>> my_list = [1, 2, 3, 4]

>>> my_list.remove(3)

>>> my_list
[1, 2, 4]

💡 Tip: This will only remove the rst occurrence of the element. For
example, if we try to remove the number 3 from a list that has two
number 3s, the second number will not be removed:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 23/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


>>> my_list = [1, 2, 3, 3, 4]

>>> my_list.remove(3)

>>> my_list
[1, 2, 3, 4]

List Indexing
We can index a list just like we index strings, with indices that start
from 0 :

>>> letters = ["a", "b", "c", "d"]

>>> letters[0]
'a'

>>> letters[1]
'b'

>>> letters[2]
'c'

>>> letters[3]
'd'

List Slicing
We can also get a slice of a list using the same syntax that we used
with strings and we can omit the parameters to use their default
values. Now, instead of adding characters to the slice, we will be
adding the elements of the list.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 24/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


<list_variable>[start:stop:step]

For example:

>>> my_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]

>>> my_list[2:6:2]
['c', 'e']

>>> my_list[2:8]
['c', 'd', 'e', 'f', 'g', 'h']

>>> my_list[1:10]
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

>>> my_list[4:8:2]
['e', 'g']

>>> my_list[::-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']

>>> my_list[::-2]
['i', 'g', 'e', 'c', 'a']

>>> my_list[8:1:-1]
['i', 'h', 'g', 'f', 'e', 'd', 'c']

List Methods
Python also has list methods already implemented to help us perform
common list operations. Here are some examples of the most
commonly used list methods:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 25/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
>>> my_list = [1, 2, 3, 3, 4]
Learn to code — free 3,000-hour curriculum
>>> my_list.append(5)
>>> my_list
[1, 2, 3, 3, 4, 5]

>>> my_list.extend([6, 7, 8])


>>> my_list
[1, 2, 3, 3, 4, 5, 6, 7, 8]

>>> my_list.insert(2, 15)


>>> my_list
[1, 2, 15, 3, 3, 4, 5, 6, 7, 8, 2, 2]

>>> my_list.remove(2)
>>> my_list
[1, 15, 3, 3, 4, 5, 6, 7, 8, 2, 2]

>>> my_list.pop()
2

>>> my_list.index(6)
6

>>> my_list.count(2)
1

>>> my_list.sort()
>>> my_list
[1, 2, 3, 3, 4, 5, 6, 7, 8, 15]

>>> my_list.reverse()
>>> my_list
[15, 8, 7, 6, 5, 4, 3, 3, 2, 1]

>>> my_list.clear()
>>> my_list
[]

To learn more about list methods, I would recommend reading this


article from the Python documentation.

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 26/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Tuples in Python
Learn to code — free 3,000-hour curriculum
To de ne a tuple in Python, we use parentheses () and separate the
elements with a comma. It is recommended to add a space after each
comma to make the code more readable.

(1, 2, 3, 4, 5)

("a", "b", "c", "d")

(3.4, 2.4, 2.6, 3.5)

We can assign tuples to variables:

my_tuple = (1, 2, 3, 4, 5)

Tuple Indexing
We can access each element of a tuple with its corresponding index:

>>> my_tuple = (1, 2, 3, 4)

>>> my_tuple[0]
1

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 27/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> my_tuple[1] Forum Donate


2
Learn to code — free 3,000-hour curriculum

>>> my_tuple[2]
3

>>> my_tuple[3]
4

We can also use negative indices:

>>> my_tuple = (1, 2, 3, 4)

>>> my_tuple[-1]
4

>>> my_tuple[-2]
3

>>> my_tuple[-3]
2

>>> my_tuple[-4]
1

Tuple Length
To nd the length of a tuple, we use the len() function, passing the
tuple as argument:

>>> my_tuple = (1, 2, 3, 4)

>>> len(my tuple)


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 28/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> len(my_tuple)
4 Forum Donate

Learn to code — free 3,000-hour curriculum

Nested Tuples
Tuples can contain values of any data type, even lists and other tuples.
These inner tuples are called nested tuples.

([1, 2, 3], (4, 5, 6))

In this example, we have a nested tuple (4, 5, 6) and a list. You can
access these nested data structures with their corresponding index.

For example:

>>> my_tuple = ([1, 2, 3], (4, 5, 6))

>>> my_tuple[0]
[1, 2, 3]

>>> my_tuple[1]
(4, 5, 6)

Tuple Slicing
We can slice a tuple just like we sliced lists and strings. The same
principle and rules apply.

This is the general syntax:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 29/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

<tuple_variable>[start:stop:step] Forum Donate

Learn to code — free 3,000-hour curriculum

For example:

>>> my_tuple = (4, 5, 6, 7, 8, 9, 10)

>>> my_tuple[3:8]
(7, 8, 9, 10)

>>> my_tuple[2:9:2]
(6, 8, 10)

>>> my_tuple[:8]
(4, 5, 6, 7, 8, 9, 10)

>>> my_tuple[:6]
(4, 5, 6, 7, 8, 9)

>>> my_tuple[:4]
(4, 5, 6, 7)

>>> my_tuple[3:]
(7, 8, 9, 10)

>>> my_tuple[2:5:2]
(6, 8)

>>> my_tuple[::2]
(4, 6, 8, 10)

>>> my_tuple[::-1]
(10, 9, 8, 7, 6, 5, 4)

>>> my_tuple[4:1:-1]
(8, 7, 6)

Tuple Methods
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 30/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

There are two built-in tuple methods in Python: Forum Donate

Learn to code — free 3,000-hour curriculum

>>> my_tuple = (4, 4, 5, 6, 6, 7, 8, 9, 10)

>>> my_tuple.count(6)
2

>>> my_tuple.index(7)
5

💡 Tip: tuples are immutable. They cannot be modi ed, so we can't


add, update, or remove elements from the tuple. If we need to do so,
then we need to create a new copy of the tuple.

Tuple Assignment
In Python, we have a really cool feature called Tuple Assignment. With
this type of assignment, we can assign values to multiple variables on
the same line.

The values are assigned to their corresponding variables in the order


that they appear. For example, in a, b = 1, 2 the value 1 is assigned
to the variable a and the value 2 is assigned to the variable b .

For example:

# Tuple Assignment
>>> a, b = 1, 2

>>> a
1

>>> b
2
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 31/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


💡 Tip: Tuple assignment is commonly used to swap the values of two
variables:

>>> a = 1

>>> b = 2

# Swap the values


>>> a, b = b, a

>>> a
2

>>> b
1

Dictionaries in Python
Now let's start diving into dictionaries. This built-in data structure lets
us create pairs of values where one value is associated with another
one.

To de ne a dictionary in Python, we use curly brackets {} with the


key-value pairs separated by a comma.

The key is separated from the value with a colon : , like this:

{"a": 1, "b": 2, "c"; 3}

You can assign the dictionary to a variable:


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 32/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


my_dict = {"a": 1, "b": 2, "c"; 3}

The keys of a dictionary must be of an immutable data type. For


example, they can be strings, numbers, or tuples but not lists since lists
are mutable.

Strings: {"City 1": 456, "City 2": 577, "City 3": 678}

Numbers: {1: "Move Left", 2: "Move Right", 3: "Move


Up", 4: "Move Down"}

Tuples: {(0, 0): "Start", (2, 4): "Goal"}

The values of a dictionary can be of any data type, so we can assign


strings, numbers, lists, tuple, sets, and even other dictionaries as the
values. Here we have some examples:

{"product_id": 4556, "ingredients": ["tomato", "cheese", "mushrooms

{"product_id": 4556, "ingredients": ("tomato", "cheese", "mushrooms"), "p

{"id": 567, "name": "Emily", "grades": {"Mathematics": 80, "Biology": 74,

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 33/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Dictionary Length
Learn to code — free 3,000-hour curriculum
To get the number of key-value pairs, we use the len() function:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> len(my_dict)
4

Get a Value in a Dictionary


To get a value in a dictionary, we use its key with this syntax:

<variable_with_dictionary>[<key>]

This expression will be replaced by the value that corresponds to the


key.

For example:

my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

print(my_dict["a"])

The output is the value associated to "a" :

1
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 34/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


Update a Value in a Dictionary
To update the value associated with an existing key, we use the same
syntax but now we add an assignment operator and the value:

<variable_with_dictionary>[<key>] = <value>

For example:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> my_dict["b"] = 6

Now the dictionary is:

{'a': 1, 'b': 6, 'c': 3, 'd': 4}

Add a Key-Value Pair to a Dictionary


The keys of a dictionary have to be unique. To add a new key-value
pair we use the same syntax that we use to update a value, but now
the key has to be new.

<variable_with_dictionary>[<new_key>] = <value>

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 35/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


For example:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> my_dict["e"] = 5

Now the dictionary has a new key-value pair:

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

Delete a Key-Value Pair in a Dictionary


To delete a key-value pair, we use the del statement:

del <dictionary_variable>[<key>]

For example:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> del my_dict["c"]

Now the dictionary is:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 36/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
{'a': 1, 'b': 2, 'd': 4}
Learn to code — free 3,000-hour curriculum

Dictionary Methods
These are some examples of the most commonly used dictionary
methods:

>>> my_dict = {"a": 1, "b": 2, "c": 3, "d": 4}

>>> my_dict.get("c")
3

>>> my_dict.items()
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])

>>> my_dict.keys()
dict_keys(['a', 'b', 'c', 'd'])

>>> my_dict.pop("d")
4

>>> my_dict.popitem()
('c', 3)

>>> my_dict.setdefault("a", 15)


1

>>> my_dict
{'a': 1, 'b': 2}

>>> my_dict.setdefault("f", 25)


25

>>> my_dict
{'a': 1, 'b': 2, 'f': 25}

>>> my_dict.update({"c": 3, "d": 4, "e": 5})

>>> my_dict.values()
dict_values([1, 2, 25, 3, 4, 5])
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 37/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
>>> my_dict.clear()
Learn to code — free 3,000-hour curriculum
>>> my_dict
{}

To learn more about dictionary methods, I recommend reading this


article from the documentation.

🔸 Python Operators
Great. Now you know the syntax of the basic data types and built-in
data structures in Python, so let's start diving into operators in
Python. They are essential to perform operations and to form
expressions.

Arithmetic Operators in Python


These operators are:

Addition: +

>>> 5 + 6
11

>>> 0 + 6
6

>>> 3.4 + 5.7


9.1

>>> "Hello" + ", " + "World"


'Hello, World'

>>> True + False


1
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 38/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum

💡 Tip: The last two examples are curious, right? This operator
behaves differently based on the data type of the operands.

When they are strings, this operator concatenates the strings and
when they are Boolean values, it performs a particular operation.

In Python, True is equivalent to 1 and False is equivalent to 0 . This


is why the result is 1 + 0 = 1

Subtraction: -

>>> 5 - 6
-1

>>> 10 - 3
7

>>> 5 - 6
-1

>>> 4.5 - 5.6 - 2.3


-3.3999999999999995

>>> 4.5 - 7
-2.5

>>> - 7.8 - 6.2


-14.0

Multiplication: *

>>> 5 * 6
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 39/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> 5 * 6
30 Forum Donate

Learn to code — free 3,000-hour curriculum


>>> 6 * 7
42

>>> 10 * 100
1000

>>> 4 * 0
0

>>> 3.4 *6.8


23.119999999999997

>>> 4 * (-6)
-24

>>> (-6) * (-8)


48

>>> "Hello" * 4
'HelloHelloHelloHello'

>>> "Hello" * 0
''

>>> "Hello" * -1
''

💡 Tip: you can "multiply" a string by an integer to repeat the string a


given number of times.

Exponentiation: **

>>> 6 ** 8
1679616

>>> 5 ** 2
25

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 40/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> 4 ** 0 Forum Donate


1
Learn to code — free 3,000-hour curriculum

>>> 16 ** (1/2)
4.0

>>> 16 ** (0.5)
4.0

>>> 125 ** (1/3)


4.999999999999999

>>> 4.5 ** 2.3


31.7971929089206

>>> 3 ** (-1)
0.3333333333333333

Division: /

>>> 25 / 5
5.0

>>> 3 / 6
0.5

>>> 0 / 5
0.0

>>> 2467 / 4673


0.5279263856195163

>>> 1 / 2
0.5

>>> 4.5 / 3.5


1.2857142857142858

>>> 6 / 7
0.8571428571428571

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 41/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> -3 / -4
0.75 Forum Donate

Learn to code — free 3,000-hour curriculum


>>> 3 / -4
-0.75

>>> -3 / 4
-0.75

💡 Tip: this operator returns a float as the result, even if the decimal
part is .0

If you try to divide by 0 , you will get a ZeroDivisionError :

>>> 5 / 0
Traceback (most recent call last):
File "<pyshell#109>", line 1, in <module>
5 / 0
ZeroDivisionError: division by zero

Integer Division: //
This operator returns an integer if the operands are integers. If they
are oats, the result will be a oat with .0 as the decimal part
because it truncates the decimal part.

>>> 5 // 6
0

>>> 8 // 2
4

>>> -4 // -5
0
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 42/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> -5 // 8
Forum Donate
-1
Learn to code — free 3,000-hour curriculum

>>> 0 // 5
0

>>> 156773 // 356


440

Modulo: %

>>> 1 % 5
1

>>> 2 % 5
2

>>> 3 % 5
3

>>> 4 % 5
4

>>> 5 % 5
0

>>> 5 % 8
5

>>> 3 % 1
0

>>> 15 % 3
0

>>> 17 % 8
1

>>> 2568 % 4
0

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 43/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> 245 % 15 Forum Donate


5
Learn to code — free 3,000-hour curriculum

>>> 0 % 6
0

>>> 3.5 % 2.4


1.1

>>> 6.7 % -7.8


-1.0999999999999996

>>> 2.3 % 7.5


2.3

Comparison Operators
These operators are:

Greater than: >

Greater than or equal to: >=

Less than: <

Less than or equal to: <=

Equal to: ==

Not Equal to: !=

These comparison operators make expressions that evaluate to either


True or False . Here we have are some examples:

>>> 5 > 6
False

>>> 10 > 8
T
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 44/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
True
Forum Donate
>>> 8 > 8
Learn to code — free 3,000-hour curriculum
False

>>> 8 >= 5
True

>>> 8 >= 8
True

>>> 5 < 6
True

>>> 10 < 8
False

>>> 8 < 8
False

>>> 8 <= 5
False

>>> 8 <= 8
True

>>> 8 <= 10
True

>>> 56 == 56
True

>>> 56 == 78
False

>>> 34 != 59
True

>>> 67 != 67
False

We can also use them to compare strings based on their alphabetical


order:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 45/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


>>> "Hello" > "World"
False
>>> "Hello" >= "World"
False
>>> "Hello" < "World"
True
>>> "Hello" <= "World"
True
>>> "Hello" == "World"
False
>>> "Hello" != "World"
True

We typically use them to compare the values of two or more


variables:

>>> a = 1
>>> b = 2

>>> a < b
True

>>> a <= b
True

>>> a > b
False

>>> a >= b
False

>>> a == b
False

>>> a != b
True

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 46/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
💡 Tip: notice that the comparison operator is == while the
Learn to code — free 3,000-hour curriculum
assignment operator is = . Their effect is different. == returns True
or False while = assigns a value to a variable.

Comparison Operator Chaining


In Python, we can use something called "comparison operator
chaining" in which we chain the comparison operators to make more
than one comparison more concisely.

For example, this checks if a is less than b and if b is less than c :

a < b < c

Here we have some examples:

>>> a = 1
>>> b = 2
>>> c = 3

>>> a < b < c


True

>>> a > b > c


False

>>> a <= b <= c


True

>>> a >= b >= c


False

>>> a >= b > c


False
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 47/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> a <= b < c Forum Donate


True
Learn to code — free 3,000-hour curriculum

Logical Operators
There are three logical operators in Python: and , or , and not . Each
one of these operators has its own truth table and they are essential
to work with conditionals.

The and operator:

>>> True and True


True

>>> True and False


False

>>> False and True


False

>>> False and False


False

The or operator:

>>> True or True


True

>>> True or False


True

>>> False or True


True

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 48/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> False or False Forum Donate


False
Learn to code — free 3,000-hour curriculum

The not operator:

>>> not True


False

>>> not False


True

These operator are used to form more complex expressions that


combine different operators and variables.

For example:

>>> a = 6
>>> b = 3

>>> a < 6 or b > 2


True

>>> a >= 3 and b >= 1


True

>>> (a + b) == 9 and b > 1


True

>>> ((a % 3) < 2) and ((a + b) == 3)


False

Assignment Operators
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 49/137
5/4/2021
g p
Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum
Assignment operators are used to assign a value to a variable. Donate

Learn to code — free 3,000-hour curriculum


They are: = , += , -= , *= , %= , /= , //= , **=

The = operator assigns the value to the variable.

The other operators perform an operation with the current


value of the variable and the new value and assigns the result
to the same variable.

For example:

>>> x = 3
>>> x
3

>>> x += 15
>>> x
18

>>> x -= 2
>>> x
16

>>> x *= 2
>>> x
32

>>> x %= 5
>>> x
2

>>> x /= 1
>>> x
2.0

>>> x //= 2
>>> x
1.0

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 50/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> x **= 5
>>> x
Forum Donate
1.0
Learn to code — free 3,000-hour curriculum

💡 Tips: these operators perform bitwise operations before assigning


the result to the variable: &= , |= , ^= , >>= , <<= .

Membership Operators
You can check if an element is in a sequence or not with the operators:
in and not in . The result will be either True or False .

For example:

>>> 5 in [1, 2, 3, 4, 5]
True

>>> 8 in [1, 2, 3, 4, 5]
False

>>> 5 in (1, 2, 3, 4, 5)
True

>>> 8 in (1, 2, 3, 4, 5)
False

>>> "a" in {"a": 1, "b": 2}


True

>>> "c" in {"a": 1, "b": 2}


False

>>> "h" in "Hello"


False

>>> "H" in "Hello"


True

5 t i [1 2 3 4 5]
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 51/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> 5 not in [1, 2, 3, 4, 5]
False Forum Donate

Learn to code — free 3,000-hour curriculum


>>> 8 not in (1, 2, 3, 4, 5)
True

>>> "a" not in {"a": 1, "b": 2}


False

>>> "c" not in {"a": 1, "b": 2}


True

>>> "h" not in "Hello"


True

>>> "H" not in "Hello"


False

We typically use them with variables that store sequences, like in this
example:

>>> message = "Hello, World!"

>>> "e" in message


True

🔹 Conditionals in Python
Now let's see how we can write conditionals to make certain parts of
our code run (or not) based on whether a condition is True or False .

if statements in Python
This is the syntax of a basic if statement:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 52/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
if <condition>:
<code> Learn to code — free 3,000-hour curriculum

If the condition is True , the code will run. Else, if it's False , the code
will not run.

💡 Tip: there is a colon ( : ) at the end of the rst line and the code is
indented. This is essential in Python to make the code belong to the
conditional.

Here we have some examples:

False Condition

x = 5

if x > 9:
print("Hello, World!")

The condition is x > 9 and the code is print("Hello, World!") .

In this case, the condition is False , so there is no output.

True Condition
Here we have another example. Now the condition is True :

color = "Blue"

if color == "Blue":
print("This is my favorite color")
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 53/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
print( This is my favorite color )
Forum Donate

Learn to code — free 3,000-hour curriculum


The output is:

"This is my favorite color"

Code After the Conditional


Here we have an example with code that runs after the conditional
has been completed. Notice that the last line is not indented, which
means that it doesn't belong to the conditional.

x = 5

if x > 9:
print("Hello!")

print("End")

In this example, the condition x > 9 is False , so the rst print


statement doesn't run but the last print statement runs because it is
not part of the conditional, so the output is:

End

However, if the condition is True , like in this example:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 54/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

x = 15 Forum Donate

Learn to code — free 3,000-hour curriculum


if x > 9:
print("Hello!")

print("End")

The output will be:

Hello!
End

Examples of Conditionals
This is another example of a conditional:

favorite_season = "Summer"

if favorite_season == "Summer":
print("That is my favorite season too!")

In this case, the output will be:

That is my favorite season too!

But if we change the value of favorite_season :

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 55/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
favorite_season = "Winter"
Learn to code — free 3,000-hour curriculum

if favorite_season == "Summer":
print("That is my favorite season too!")

There will be no output because the condition will be False .

if/else statements in Python


We can add an else clause to the conditional if we need to specify
what should happen when the condition is False .

This is the general syntax:

if <condition>:
<code>
else:
<code>

💡 Tip: notice that the two code blocks are indented ( if and else ).
This is essential for Python to be able to differentiate between the
code that belongs to the main program and the code that belongs to
the conditional.

Let's see an example with the else clause:

True Condition

x = 15

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 56/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

if x > 9: Forum Donate


print("Hello!")
else: Learn to code — free 3,000-hour curriculum
print("Bye!")

print("End")

The output is:

Hello!
End

When the condition of the if clause is True , this clause runs. The el
se clause doesn't run.

False Condition
Now the else clause runs because the condition is False .

x = 5

if x > 9:
print("Hello!")
else:
print("Bye!")

print("End")

Now the output is:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 57/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Bye!
Forum Donate
End
Learn to code — free 3,000-hour curriculum

if/elif/else statements in Python


To customize our conditionals even further, we can add one or more
elif clauses to check and handle multiple conditions. Only the code
of the rst condition that evaluates to True will run.

💡 Tip: elif has to be written after if and before else .

First Condition True

x = 5

if x < 9:
print("Hello!")
elif x < 15:
print("It's great to see you")
else:
print("Bye!")

print("End")

We have two conditions x < 9 and x < 15 . Only the code block from
the rst condition that is True from top to bottom will be executed.

In this case, the output is:

Hello!
End

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 58/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Because the rst condition is True : x < 9 .
Learn to code — free 3,000-hour curriculum

Second Condition True


If the rst condition is False , then the second condition will be
checked.

In this example, the rst condition x < 9 is False but the second
condition x < 15 is True , so the code that belongs to this clause will
run.

x = 13

if x < 9:
print("Hello!")
elif x < 15:
print("It's great to see you")
else:
print("Bye!")

print("End")

The output is:

It's great to see you


End

All Conditions are False


If all conditions all False , then the else clause will run:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 59/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
x = 25
Learn to code — free 3,000-hour curriculum
if x < 9:
print("Hello!")
elif x < 15:

print("It's great to see you")


else:
print("Bye!")

print("End")

The output will be:

Bye!
End

Multiple elif Clauses


We can add as many elif clauses as needed. This is an example of a
conditional with two elif clauses:

if favorite_season == "Winter":
print("That is my favorite season too")
elif favorite_season == "Summer":
print("Summer is amazing")
elif favorite_season == "Spring":
print("I love spring")
else:
print("Fall is my mom's favorite season")

Each condition will be checked and only the code block of the rst
diti th t l t t T ill If f th T
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 60/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
condition that evaluates to True will run. If none of them are True ,
Forum Donate
the else clause will run.
Learn to code — free 3,000-hour curriculum

🔸 For Loops in Python


Now you know how to write conditionals in Python, so let's start
diving into loops. For loops are amazing programming structures that
you can use to repeat a code block a speci c number of times.

This is the basic syntax to write a for loop in Python:

for <loop_variable> in <iterable>:


<code>

The iterable can be a list, tuple, dictionary, string, the sequence


returned by range, a le, or any other type of iterable in Python. We
will start with range() .

The range() function in Python


This function returns a sequence of integers that we can use to
determine how many iterations (repetitions) of the loop will be
completed. The loop will complete one iteration per integer.

💡 Tip: Each integer is assigned to the loop variable one at a time per
iteration.

This is the general syntax to write a for loop with range() :

for <loop_variable> in range(<start>, <stop>, <step>):


<code>
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 61/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


As you can see, the range function has three parameters:

start : where the sequence of integers will start. By default,

it's 0 .

stop : where the sequence of integers will stop (without


including this value).

step : the value that will be added to each element to get the
next element in the sequence. By default, it's 1 .

You can pass 1, 2, or 3 arguments to range() :

With 1 argument, the value is assigned to the stop parameter


and the default values for the other two parameters are used.

With 2 arguments, the values are assigned to the start and


stop parameters and the default value for step is used.

With 3 arguments, the values are assigned to the start , stop ,


and step parameters (in order).

Here we have some examples with one parameter:

for i in range(5):
print(i)

Output:

0
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 62/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

1
Forum Donate
2
3 Learn to code — free 3,000-hour curriculum
4

💡 Tip: the loop variable is updated automatically.

>>> for j in range(15):


print(j * 2)

Output:

0
2
4
6
8
10
12
14
16
18
20
22
24
26
28

In the example below, we repeat a string as many times as indicated by


the value of the loop variable:

>>> f i (8)
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 63/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> for num in range(8):
print("Hello" * num) Forum Donate

Learn to code — free 3,000-hour curriculum

Output:

Hello
HelloHello
HelloHelloHello
HelloHelloHelloHello
HelloHelloHelloHelloHello
HelloHelloHelloHelloHelloHello
HelloHelloHelloHelloHelloHelloHello

We can also use for loops with built-in data structures such as lists:

>>> my_list = ["a", "b", "c", "d"]

>>> for i in range(len(my_list)):


print(my_list[i])

Output:

a
b
c
d

💡 Tip: when you use range(len(<seq>)) , you get a sequence of


numbers that goes from 0 up to len(<seq>)-1 . This represents the
sequence of valid indices.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 64/137
5/4/2021
q Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
These are some examples with two parameters:
Learn to code — free 3,000-hour curriculum

>>> for i in range(2, 10):

print(i)

Output:

2
3
4
5
6
7
8
9

Code:

>>> for j in range(2, 5):


print("Python" * j)

Output:

PythonPython
PythonPythonPython
PythonPythonPythonPython

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 65/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Code: Learn to code — free 3,000-hour curriculum

>>> my_list = ["a", "b", "c", "d"]

>>> for i in range(2, len(my_list)):


print(my_list[i])

Output:

c
d

Code:

>>> my_list = ["a", "b", "c", "d"]

>>> for i in range(2, len(my_list)-1):


my_list[i] *= i

Now the list is: ['a', 'b', 'cc', 'd']

These are some examples with three parameters:

>>> for i in range(3, 16, 2):


print(i)

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 66/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Output:
Learn to code — free 3,000-hour curriculum

3
5
7

9
11
13
15

Code:

>>> for j in range(10, 5, -1):


print(j)

Output:

10
9
8
7
6

Code:

>>> my_list = ["a", "b", "c", "d", "e", "f", "g"]

>>> for i in range(len(my list) 1 2 1):


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 67/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> for i in range(len(my_list)-1, 2, -1):
print(my_list[i]) Forum Donate

Learn to code — free 3,000-hour curriculum

Output:

f
e
d

How to Iterate over Iterables in Python


We can iterate directly over iterables such as lists, tuples, dictionaries,
strings, and les using for loops. We will get each one of their
elements one at a time per iteration. This is very helpful to work with
them directly.

Let's see some examples:

Iterate Over a String


If we iterate over a string, its characters will be assigned to the loop
variable one by one (including spaces and symbols).

>>> message = "Hello, World!"

>>> for char in message:


print(char)

H
e
l
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 68/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

l Forum Donate
o
, Learn to code — free 3,000-hour curriculum

W
o
r
l
d
!

We can also iterate over modi ed copies of the string by calling a


string method where we specify the iterable in the for loop. This will
assign the copy of the string as the iterable that will be used for the
iterations, like this:

>>> word = "Hello"

>>> for char in word.lower(): # calling the string method


print(char)

h
e
l
l
o

>>> word = "Hello"

>>> for char in word.upper(): # calling the string method


print(char)

H
E
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 69/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

L Forum Donate
L
O Learn to code — free 3,000-hour curriculum

Iterate Over Lists and Tuples

>>> my_list = [2, 3, 4, 5]

>>> for num in my_list:


print(num)

The output is:

2
3
4
5

Code:

>>> my_list = (2, 3, 4, 5)

>>> for num in my_list:


if num % 2 == 0:
print("Even")
else:
print("Odd")

Output:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 70/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Even Learn to code — free 3,000-hour curriculum


Odd
Even
Odd

Iterate Over the Keys, Values, and Key-


Value Pairs of Dictionaries
We can iterate over the keys, values, and key-value pairs of a
dictionary by calling speci c dictionary methods. Let's see how.

To iterate over the keys, we write:

for <var> in <dictionary_variable>:


<code>

We just write the name of the variable that stores the dictionary as
the iterable.

💡 Tip: you can also write <dictionary_variable>.keys() but writing


the name of the variable directly is more concise and it works exactly
the same.

For example:

>>> my_dict = {"a": 1, "b": 2, "c": 3}

>>> for key in my_dict:


print(key)

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 71/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
a
b Learn to code — free 3,000-hour curriculum
c

💡 Tip: you can assign any valid name to the loop variable.

To iterate over the values, we use:

for <var> in <dictionary_variable>.values():


<code>

For example:

>>> my_dict = {"a": 1, "b": 2, "c": 3}

>>> for value in my_dict.values():


print(value)

1
2
3

To iterate over the key-value pairs, we use:

for <key>, <value> in <dictionary_variable>.items():


<code>

💡 Tip: we are de ning two loop variables because we want to assign


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 72/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
💡 Tip: we are de ning two loop variables because we want to assign
the key and the value to variables that we can use in the loop.
Forum Donate

Learn to code — free 3,000-hour curriculum

>>> my_dict = {"a": 1, "b": 2, "c": 3}

>>> for key, value in my_dict.items():


print(key, value)

a 1
b 2
c 3

If we de ne only one loop variable, this variable will contain a tuple


with the key-value pair:

>>> my_dict = {"a": 1, "b": 2, "c": 3}


>>> for pair in my_dict.items():
print(pair)

('a', 1)
('b', 2)
('c', 3)

Break and Continue in Python


Now you know how to iterate over sequences in Python. We also have
loop control statements to customize what happens when the loop
runs: break and continue .

The Break Statement


The break statement is used to stop the loop immediately
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 73/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
The break statement is used to stop the loop immediately.
Forum Donate

When a break Learn


statement is —
to code found, the loop stops
free 3,000-hour and the program
curriculum
returns to its normal execution beyond the loop.

In the example below, we stop the loop when an even element is


found.

>>> my_list = [1, 2, 3, 4, 5]

>>> for elem in my_list:


if elem % 2 == 0:
print("Even:", elem)
print("break")
break
else:
print("Odd:", elem)

Odd: 1
Even: 2
break

The Continue Statement


The continue statement is used to skip the rest of the current
iteration.

When it is found during the execution of the loop, the current


iteration stops and a new one begins with the updated value of the
loop variable.

In the example below, we skip the current iteration if the element is


even and we only print the value if the element is odd:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 74/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
>>> my_list = [1, 2, 3, 4, 5]
Learn to code — free 3,000-hour curriculum
>>> for elem in my_list:
if elem % 2 == 0:
print("continue")
continue
print("Odd:", elem)

Odd: 1
continue
Odd: 3
continue
Odd: 5

The zip() function in Python


zip() is an amazing built-in function that we can use in Python to
iterate over multiple sequences at once, getting their corresponding
elements in each iteration.

We just need to pass the sequences as arguments to the zip()


function and use this result in the loop.

For example:

>>> my_list1 = [1, 2, 3, 4]


>>> my_list2 = [5, 6, 7, 8]

>>> for elem1, elem2 in zip(my_list1, my_list2):


print(elem1, elem2)

1 5
2 6
3 7
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 75/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

4 8
Forum Donate

Learn to code — free 3,000-hour curriculum

The enumerate() Function in Python


You can also keep track of a counter while the loop runs with the enum
() function. It is commonly used to iterate over a sequence and get
the corresponding index.

💡 Tip: By default, the counter starts at 0 .

For example:

>>> my_list = [5, 6, 7, 8]

>>> for i, elem in enumerate(my_list):


print(i, elem)

0 5
1 6
2 7
3 8

>>> word = "Hello"

>>> for i, char in enumerate(word):


print(i, char)

0 H
1 e
2 l
3 l
4 o

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 76/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
If you start the Learn
counter from—0free
to code , you can use the
3,000-hour index and the current
curriculum
value in the same iteration to modify the sequence:

>>> my_list = [5, 6, 7, 8]

>>> for index, num in enumerate(my_list):


my_list[index] = num * 3

>>> my_list
[15, 18, 21, 24]

You can start the counter from a different number by passing a second
argument to enumerate() :

>>> word = "Hello"

>>> for i, char in enumerate(word, 2):


print(i, char)

2 H
3 e
4 l
5 l
6 o

The else Clause


For loops also have an else clause. You can add this clause to the loop
if you want to run a speci c block of code when the loop completes all
its iterations without nding the break statement.

💡 Ti if i f d h l d ' d if i
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 77/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

💡 Tip: if break is found, the else clause doesn't run and if break is
Forum Donate
not found, the else clause runs.
Learn to code — free 3,000-hour curriculum

In the example below, we try to nd an element greater than 6 in the


list. That element is not found, so break doesn't run and the else
clause runs.

my_list = [1, 2, 3, 4, 5]

for elem in my_list:


if elem > 6:
print("Found")
break
else:
print("Not Found")

The output is:

Not Found

However, if the break statement runs, the else clause doesn't run.
We can see this in the example below:

my_list = [1, 2, 3, 4, 5, 8] # Now the list has the value 8

for elem in my_list:


if elem > 6:
print("Found")
break
else:
print("Not Found")

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 78/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

The output is: Learn to code — free 3,000-hour curriculum

Found

🔹 While Loops in Python


While loops are similar to for loops in that they let us repeat a block of
code. The difference is that while loops run while a condition is True .

In a while loop, we de ne the condition, not the number of iterations.


The loop stops when the condition is False .

This is the general syntax of a while loop:

while <condition>:
<code>

💡 Tip: in while loops, you must update the variables that are part of
the condition to make sure that the condition will eventually become
False .

For example:

>>> x = 6

>>> while x < 15:


print(x)
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 79/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

x += 1
Forum Donate

Learn to code — free 3,000-hour curriculum


6
7
8
9
10
11
12
13
14

>>> x = 4

>>> while x >= 0:


print("Hello" * x)
x -= 1

HelloHelloHelloHello
HelloHelloHello
HelloHello
Hello

>>> num = 5

>>> while num >= 1:


print("*" * num)
num -= 2

*****
***
*

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 80/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum
Break and Continue Donate

We can also useLearn to code


break and — free 3,000-hour
continue curriculum
with while loops and they both
work exactly the same:

break stops the while loop immediately.

continue stops the current iteration and starts the next one.

For example:

>>> x = 5

>>> while x < 15:


if x % 2 == 0:
print("Even:", x)
break
print(x)
x += 1

5
Even: 6

>>> x = 5

>>> while x < 15:


if x % 2 == 0:
x += 1
continue
print("Odd:", x)
x += 1

Odd: 5
Odd: 7
Odd: 9
Odd: 11
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 81/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
Odd: 11
Odd: 13 Forum Donate

Learn to code — free 3,000-hour curriculum

The else Clause


We can also add an else clause to a while loop. If break is found, the
else clause doesn't run but if the break statement is not found, the
else clause runs.

In the example below, the break statement is not found because none
of the numbers are even before the condition becomes False , so the
else clause runs.

x = 5

while x < 15:


if x % 2 == 0:
print("Even number found")
break
print(x)
x += 2
else:
print("All numbers were odd")

This is the output:

5
7
9
11
13
All numbers were odd

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 82/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
But in this version of the example, the break statement is found and
the else clauseLearn to code
doesn't run:— free 3,000-hour curriculum

x = 5

while x < 15:


if x % 2 == 0:
print("Even number found")
break

print(x)
x += 1 # Now we are incrementing the value by 1
else:
print("All numbers were odd")

The output is:

5
Even number found

Infinite While Loops


When we write and work with while loops, we can have something
called an "in nite loop." If the condition is never False , the loop will
never stop without external intervention.

This usually happens when the variables in the condition are not
updated properly during the execution of the loop.

💡 Tip: you must make the necessary updates to these variables to


make sure that the condition will eventually evaluate to False .

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 83/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

For example: Forum Donate

Learn to code — free 3,000-hour curriculum

>>> x = 5

>>> while x > 2:


print(x)

5
5
5

5
5
5
5
5
5
.
.
.
# The output continues indefinitely

💡 Tip: to stop this process, type CTRL + C . You should see a Keyboard
Interrupt message.

🔸 Nested Loops in Python


We can write for loops within for loops and while loops within while
loops. These inner loops are called nested loops.

💡 Tip: the inner loop runs for each iteration of the outer loop.

Nested For Loops in Python

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 84/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> for i in range(3):


for j in range(2):
Forum Donate
print(i, j)
Learn to code — free 3,000-hour curriculum

0 0
0 1
1 0
1 1
2 0
2 1

If we add print statements, we can see what is happening behind the


scenes:

>>> for i in range(3):


print("===> Outer Loop")
print(f"i = {i}")
for j in range(2):
print("Inner Loop")
print(f"j = {j}")

===> Outer Loop


i = 0
Inner Loop
j = 0
Inner Loop
j = 1
===> Outer Loop
i = 1
Inner Loop
j = 0
Inner Loop
j = 1
===> Outer Loop
i = 2
Inner Loop
j = 0
Inner Loop
j = 1

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 85/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


The inner loop completes two iterations per iteration of the outer
loop. The loop variables are updated when a new iteration starts.

This is another example:

>>> num_rows = 5

>>> for i in range(5):


for num_cols in range(num_rows-i):
print("*", end="")
print()

*****
****
***
**
*

Nested While Loops in Python


Here we have an example of nested while loops. In this case, we have
to update the variables that are part of each condition to guarantee
that the loops will stop.

>>> i = 5

>>> while i > 0:


j = 0
while j < 2:
print(i, j)
j += 1
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 86/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
j
i -= 1 Forum Donate

Learn to code — free 3,000-hour curriculum


5 0
5 1
4 0
4 1
3 0
3 1
2 0
2 1
1 0
1 1

💡 Tip: we can also have for loops within while loops and while loops
within for loops.

🔹 Functions in Python
In Python, we can de ne functions to make our code reusable, more
readable, and organized. This is the basic syntax of a Python function:

def <function_name>(<param1>, <param2>, ...):


<code>

💡 Tip: a function can have zero, one, or multiple parameters.

Function with No Parameters in Python


A function with no parameters has an empty pair of parentheses after
its name in the function de nition. For example:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 87/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

def print_pattern(): Forum Donate


size = 4
for i in range(size):
Learn to code — free 3,000-hour curriculum
print("*" * size)

This is the output when we call the function:

>>> print_pattern()
****

****
****
****

💡 Tip: You have to write an empty pair of parentheses after the name
of the function to call it.

Function with One Parameter in Python


A function with one or more parameters has a list of parameters
surrounded by parentheses after its name in the function de nition:

def welcome_student(name):
print(f"Hi, {name}! Welcome to class.")

When we call the function, we just need to pass one value as argument
and that value will be replaced where we use the parameter in the
function de nition:

>>> welcome student("Nora")


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 88/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
>>> welcome_student( Nora )
Hi, Nora! Welcome to class. Forum Donate

Learn to code — free 3,000-hour curriculum

Here we have another example – a function that prints a pattern made


with asterisks. You have to specify how many rows you want to print:

def print_pattern(num_rows):
for i in range(num_rows):
for num_cols in range(num_rows-i):
print("*", end="")
print()

You can see the different outputs for different values of num_rows :

>>> print_pattern(3)
***
**
*

>>> print_pattern(5)
*****
****
***
**
*

>>> print_pattern(8)
********
*******
******
*****
****
***
**
*

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 89/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

FunctionsLearn
with Two
to code or3,000-hour
— free More curriculum
Parameters in
Python
To de ne two or more parameters, we just separate them with a
comma:

def print_sum(a, b):


print(a + b)

Now when we call the function, we must pass two arguments:

>>> print_sum(4, 5)
9

>>> print_sum(8, 9)
17

>>> print_sum(0, 0)
0

>>> print_sum(3, 5)
8

We can adapt the function that we just saw with one parameter to
work with two parameters and print a pattern with a customized
character:

def print_pattern(num_rows, char):


for i in range(num_rows):
for num_cols in range(num_rows-i):
print(char, end="")
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 90/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

print()
Forum Donate

Learn to code — free 3,000-hour curriculum

You can see the output with the customized character is that we call
the function passing the two arguments:

>>> print_pattern(5, "A")


AAAAA
AAAA
AAA
AA
A

>>> print_pattern(8, "%")


%%%%%%%%
%%%%%%%
%%%%%%
%%%%%
%%%%
%%%
%%
%

>>> print_pattern(10, "#")


##########
#########
########
#######
######
#####
####
###
##
#

How to Return a Value in Python


Awesome. Now you know how to de ne a function, so let's see how
you can work with return statements.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 91/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
We will often need to return a value from a function. We can do this
Learn to code — free 3,000-hour curriculum
with the return statement in Python. We just need to write this in
the function de nition:

return <value_to_return>

💡 Tip: the function stops immediately when return is found and the

value is returned.

Here we have an example:

def get_rectangle_area(length, width):


return length * width

Now we can call the function and assign the result to a variable
because the result is returned by the function:

>>> area = get_rectangle_area(4, 5)


>>> area
20

We can also use return with a conditional to return a value based on


whether a condition is True or False .

In this example, the function returns the rst even element found in
the sequence:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 92/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


def get_first_even(seq):
for elem in seq:
if elem % 2 == 0:
return elem
else:
return None

If we call the function, we can see the expected results:

>>> value1 = get_first_even([2, 3, 4, 5])


>>> value1
2

>>> value2 = get_first_even([3, 5, 7, 9])


>>> print(value2)
None

💡 Tip: if a function doesn't have a return statement or doesn't nd


one during its execution, it returns None by default.

The Style Guide for Python Code recommends using return


statements consistently. It mentions that we should:

Be consistent in return statements. Either all return


statements in a function should return an expression, or none
of them should. If any return statement returns an expression,
any return statements where no value is returned should
explicitly state this as return None, and an explicit return
statement should be present at the end of the function (if
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 93/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
statement should be present at the end of the function (if
reachable)
Forum Donate

Learn to code — free 3,000-hour curriculum

Default Arguments in Python


We can assign default arguments for the parameters of our function.
To do this, we just need to write <parameter>=<value> in the list of
parameters.

💡 Tip: The Style Guide for Python Code mentions that we shouldn't
"use spaces around the = sign when used to indicate a keyword

argument."

In this example, we assign the default value 5 to the parameter b . If


we omit this value when we call the function, the default value will be
used.

def print_product(a, b=5):


print(a * b)

If we call the function without this argument, you can see the output:

>>> print_product(4)
20

We con rm that the default argument 5 was used in the operation.

But we can also assign a custom value for b by passing a second


argument:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 94/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
>>> print_product(3, 4)
12 Learn to code — free 3,000-hour curriculum

💡 Tip: parameters with default arguments have to be de ned at the


end of the list of parameters. Else, you will see this error: SyntaxErro
r: non-default argument follows default argument .

Here we have another example with the function that we wrote to

print a pattern. We assign the default value "*" to the char


parameter.

def print_pattern(num_rows, char="*"):


for i in range(num_rows):
for num_cols in range(num_rows-i):
print(char, end="")
print()

Now we have the option to use the default value or customize it:

>>> print_pattern(5)
*****
****
***
**
*

>>> print_pattern(6, "&")


&&&&&&
&&&&&
&&&&
&&&
&&
&
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 95/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum

🔸 Recursion in Python
A recursive function is a function that calls itself. These functions have
a base case that stops the recursive process and a recursive case that
continues the recursive process by making another recursive call.

Here we have some examples in Python:

def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)

Recursive Factorial Function

def fibonacci(n):
if n == 0 or n == 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

The Fibonacci Function

def find_power(a, b):


if b == 0:
return 1
else:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 96/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
else:
return a * find_power(a, b-1) Forum Donate

Learn to code — free 3,000-hour curriculum


Find a Power Recursively

🔹 Exception Handling in Python


An error or unexpected event that that occurs while a program is
running is called an exception. Thanks to the elements that we will see

in just a moment, we can avoid terminating the program abruptly


when this occurs.
Let's see the types of exceptions in Python and how we can handle
them.

Common Exceptions in Python


This is a list of common exceptions in Python and why they occur:

ZeroDivisionError: raised when the second argument of a


division or modulo operation is zero.

>>> 5 / 0
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
5 / 0
ZeroDivisionError: division by zero

>>> 7 // 0
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
7 // 0
ZeroDivisionError: integer division or modulo by zero

>>> 8 % 0
Traceback (most recent call last):
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 97/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module> Forum Donate
8 % 0
Learninteger
ZeroDivisionError: to code division
— free 3,000-hour
or modulo curriculum
by zero

IndexError: raised when we try to use an invalid index to


access an element of a sequence.

>>> my_list = [3, 4, 5, 6]

>>> my_list[15]
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
my_list[15]
IndexError: list index out of range

KeyError: raised when we try to access a key-value pair that


doesn't exist because the key is not in the dictionary.

>>> my_dict = {"a": 1, "b": 2, "c": 3}

>>> my_dict["d"]
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
my_dict["d"]
KeyError: 'd'

NameError: raised when we use a variable that has not been


de ned previously.

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 98/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> b Forum Donate


Traceback (most recent call last):
Learn to line
File "<pyshell#8>", code 1,
— free 3,000-hour curriculum
in <module>
b
NameError: name 'b' is not defined

RecursionError: raised when the interpreter detects that the


maximum recursion depth is exceeded. This usually occurs
when the process never reaches the base case.

In the example below, we will get a RecursionError . The factorial


function is implemented recursively but the argument passed to the
recursive call is n instead of n-1 . Unless the value is already 0 or 1 ,
the base case will not be reached because the argument is not being
decremented, so the process will continue and we will get this error.

>>> def factorial(n):


if n == 0 or n == 1:
return 1
else:
return n * factorial(n)

>>> factorial(5)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
factorial(5)
File "<pyshell#5>", line 5, in factorial
return n * factorial(n)
File "<pyshell#5>", line 5, in factorial
return n * factorial(n)
File "<pyshell#5>", line 5, in factorial
return n * factorial(n)
[Previous line repeated 1021 more times]
File "<pyshell#5>", line 2, in factorial
if n == 0 or n == 1:
RecursionError: maximum recursion depth exceeded in comparison

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 99/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


💡 Tip: to learn more about these exceptions, I recommend reading
this article from the documentation.

try / except in Python


We can use try/except in Python to catch the exceptions when they
occur and handle them appropriately. This way, the problem can
terminate appropriately or even recover from the exception.

This is the basic syntax:

try:
<code_that_may_raise_an_exception>
except:
<code_to_handle_the_exception_if_it_occurs>

For example, if we take user input to access an element in a list, the


input might not be a valid index, so an exception could be raised:

index = int(input("Enter the index: "))

try:
my_list = [1, 2, 3, 4]
print(my_list[index])
except:
print("Please enter a valid index.")

If we enter an invalid value like 15, the output will be:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 100/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Please enter a valid index.
Learn to code — free 3,000-hour curriculum

Because the except clause runs. However, if the value is valid, the
code in try will run as expected.

Here we have another example:

a = int(input("Enter a: "))
b = int(input("Enter b: "))

try:
division = a / b
print(division)
except:
print("Please enter valid values.")

The output is:

Enter a: 5
Enter b: 0

Please enter valid values.

How to Catch a Specific Type of Exception


in Python
Instead of catching and handling all possible exceptions that could
occur in the try clause, we could catch and handle a speci c type of
exception. We just need to specify the type of the exception after the
except keyword:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 101/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


try:
<code_that_may_raise_an_exception>
except <exception_type>:
<code_to_handle_an_exception_if_it_occurs>

For example:

index = int(input("Enter the index: "))

try:
my_list = [1, 2, 3, 4]
print(my_list[index])
except IndexError: # specify the type
print("Please enter a valid index.")

a = int(input("Enter a: "))
b = int(input("Enter b: "))

try:
division = a / b
print(division)
except ZeroDivisionError: # specify the type
print("Please enter valid values.")

How to Assign a Name to the Exception


Object in Python
We can specify a name for the exception object by assigning it to a
variable that we can use in the except clause. This will let us access
its description and attributes.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 102/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
We only need to add as <name> , like this:
Learn to code — free 3,000-hour curriculum

try:
<code_that_may_raise_an_exception>
except <exception_type> as <name>:
<code_to_handle_an_exception_if_it_occurs>

For example:

index = int(input("Enter the index: "))

try:
my_list = [1, 2, 3, 4]
print(my_list[index])
except IndexError as e:
print("Exception raised:", e)

This is the output if we enter 15 as the index:

Enter the index: 15


Exception raised: list index out of range

This is another example:

a = int(input("Enter a: "))
b = int(input("Enter b: "))

try:
di i i / b
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 103/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
division = a / b
print(division) Forum Donate
except ZeroDivisionError as err:
Learn
print("Please to code
enter — free
valid 3,000-hour
values.", err) curriculum

This is the output if we enter the value 0 for b :

Please enter valid values. division by zero

try / except / else in Python


We can add an else clause to this structure after except if we want
to choose what happens when no exceptions occur during the
execution of the try clause:

try:
<code_that_may_raise_an_exception>
except:
<code_to_handle_an_exception_if_it_occurs>
else:
<code_that_only_runs_if_no_exception_in_try>

For example:

a = int(input("Enter a: "))
b = int(input("Enter b: "))

try:
division = a / b
print(division)
except ZeroDivisionError as err:
print("Please enter valid values " err)
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 104/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
print( Please enter valid values. , err)
else: Forum Donate
print("Both values were valid.")
Learn to code — free 3,000-hour curriculum

If we enter the values 5 and 0 for a and b respectively, the output


is:

Please enter valid values. division by zero

But if both values are valid, for example 5 and 4 for a and b
respectively, the else clause runs after try is completed and we see:

1.25
Both values were valid.

try / except / else / finally in Python


We can also add a finally clause if we need to run code that should
always run, even if an exception is raised in try .

For example:

a = int(input("Enter a: "))
b = int(input("Enter b: "))

try:
division = a / b
print(division)
except ZeroDivisionError as err:
print("Please enter valid values.", err)
else:
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 105/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
else:
print("Both values were valid.") Forum Donate
finally:
Learn to code — free 3,000-hour curriculum
print("Finally!")

If both values are valid, the output is the result of the division and:

Both values were valid.


Finally!

And if an exception is raised because b is 0 , we see:

Please enter valid values. division by zero


Finally!

The finally clause always runs.

💡 Tip: this clause can be used, for example, to close les even if the
code throws an exception.

🔸 Object-Oriented Programming
in Python
In Object-Oriented Programming (OOP), we de ne classes that act as
blueprints to create objects in Python with attributes and methods
(functionality associated with the objects).

This is a general syntax to de ne a class:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 106/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
class <className>:
Learn to code — free 3,000-hour curriculum
<class_attribute_name> = <value>

def __init__(self,<param1>, <param2>, ...):


self.<attr1> = <param1>
self.<attr2> = <param2>
.
.
.
# As many attributes as needed

def <method_name>(self, <param1>, ...):


<code>

# As many methods as needed

💡 Tip: self refers to an instance of the class (an object created with
the class blueprint).

As you can see, a class can have many different elements so let's
analyze them in detail:

Class Header
The rst line of the class de nition has the class keyword and the
name of the class:

class Dog:

class House:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 107/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


class Ball:

💡 Tip: If the class inherits attributes and methods from another class,
we will see the name of the class within parentheses:

class Poodle(Dog):

class Truck(Vehicle):

class Mom(FamilyMember):

In Python, we write class name in Upper Camel Case (also known as


Pascal Case), in which each word starts with an uppercase letter. For
example: FamilyMember

__init__ and instance attributes


We are going to use the class to create object in Python, just like we
build real houses from blueprints.

The objects will have attributes that we de ne in the class. Usually, we


initialize these attributes in __init__ . This is a method that runs
when we create an instance of the class.

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 108/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

This is the general syntax: Forum Donate

Learn to code — free 3,000-hour curriculum

def __init__(self, <parameter1>, <parameter2>, ...):


self.<attribute1> = <parameter1> # Instance attribute
self.<attribute2> = <parameter2> # Instance attribute
.
.
.
# As many instance attributes as needed

We specify as many parameters as needed to customize the values of


the attributes of the object that will be created.
Here is an example of a Dog class with this method:

class Dog:

def __init__(self, name, age):


self.name = name
self.age = age

💡 Tip: notice the double leading and trailing underscore in the name
__init__ .

How to Create an Instance


To create an instance of Dog , we need to specify the name and age of
the dog instance to assign these values to the attributes:

my_dog = Dog("Nora", 10)

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 109/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Great. Now weLearn to code


have our — freeready
instance 3,000-hour
to be curriculum
used in the program.

Some classes will not require any arguments to create an instance. In


that case, we just write empty parentheses. For example:

class Circle:

def __init__(self):
self.radius = 1

To create an instance:

>>> my_circle = Circle()

💡 Tip: self is like a parameter that acts "behind the scenes", so even
if you see it in the method de nition, you shouldn't consider it when
you pass the arguments.

Default Arguments
We can also assign default values for the attributes and give the
option to the user if they would like to customize the value.

In this case, we would write <attribute>=<value> in the list of


parameters.

This is an example:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 110/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
class Circle:
Learn to code — free 3,000-hour curriculum
def __init__(self, radius=1):
self.radius = radius

Now we can create a Circle instance with the default value for the
radius by omitting the value or customize it by passing a value:

# Default value

>>> my_circle1 = Circle()

# Customized value
>>> my_circle2 = Circle(5)

How to Get an Instance Attribute


To access an instance attribute, we use this syntax:

<object_variable>.<attribute>

For example:

# Class definition
>>> class Dog:

def __init__(self, name, age):


self.name = name
self.age = age

# Create instance
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 111/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
# Create instance
>>> my_dog = Dog("Nora", 10) Forum Donate

Learn to code — free 3,000-hour curriculum


# Get attributes
>>> my_dog.name
'Nora'

>>> my_dog.age
10

How to Update an Instance Attribute


To update an instance attribute, we use this syntax:

<object_variable>.<attribute> = <new_value>

For example:

>>> class Dog:

def __init__(self, name, age):


self.name = name
self.age = age

>>> my_dog = Dog("Nora", 10)

>>> my_dog.name
'Nora'

# Update the attribute


>>> my_dog.name = "Norita"

>>> my_dog.name
'Norita'

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 112/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


How to Remove an Instance Attribute
To remove an instance attribute, we use this syntax:

del <object_variable>.<attribute>

For example:

>>> class Dog:

def __init__(self, name, age):


self.name = name
self.age = age

>>> my_dog = Dog("Nora", 10)

>>> my_dog.name
'Nora'

# Delete this attribute


>>> del my_dog.name

>>> my_dog.name
Traceback (most recent call last):
File "<pyshell#77>", line 1, in <module>
my_dog.name
AttributeError: 'Dog' object has no attribute 'name'

How to Delete an Instance


Similarly, we can delete an instance using del :

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 113/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

>>> class Dog:


Learn to code — free 3,000-hour curriculum

def __init__(self, name, age):


self.name = name
self.age = age

>>> my_dog = Dog("Nora", 10)

>>> my_dog.name
'Nora'

# Delete the instance


>>> del my_dog

>>> my_dog
Traceback (most recent call last):
File "<pyshell#79>", line 1, in <module>
my_dog
NameError: name 'my_dog' is not defined

Public vs. Non-Public Attributes in Python


In Python, we don't have access modi ers to functionally restrict
access to the instance attributes, so we rely on naming conventions to
specify this.

For example, by adding a leading underscore, we can signal to other


developers that an attribute is meant to be non-public.

For example:

class Dog:

def __init__(self, name, age):


self.name = name # Public attribute
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 114/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
self.name name # Public attribute
self._age = age # Non-Public attribute Forum Donate

Learn to code — free 3,000-hour curriculum

The Python documentation mentions:

Use one leading underscore only for non-public methods and


instance variables.

Always decide whether a class's methods and instance


variables (collectively: "attributes") should be public or non-
public. If in doubt, choose non-public; it's easier to make it

public later than to make a public attribute non-public.

Non-public attributes are those that are not intended to be


used by third parties; you make no guarantees that non-public
attributes won't change or even be removed. - source

However, as the documentation also mentions:

We don't use the term "private" here, since no attribute is


really private in Python (without a generally unnecessary
amount of work). - source

💡 Tip: technically, we can still access and modify the attribute if we


add the leading underscore to its name, but we shouldn't.

Class Attributes in Python


Class attributes are shared by all instances of the class. They all have
access to this attribute and they will also be affected by any changes
made to these attributes.

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 115/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
class Dog:

Learn to code — free 3,000-hour curriculum


# Class attributes
kingdom = "Animalia"
species = "Canis lupus"

def __init__(self, name, age):


self.name = name
self.age = age

💡 Tip: usually, they are written before the __init__ method.

How to Get a Class Attribute


To get the value of a class attribute, we use this syntax:

<class_name>.<attribute>

For example:

>>> class Dog:

kingdom = "Animalia"

def __init__(self, name, age):


self.name = name
self.age = age

>>> Dog.kingdom
'Animalia'

💡 Tip: You can use this syntax within the class as well.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 116/137
💡 Tip: You can use this syntax within the class as well.
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

How to Update a Class Attribute


Learn to code — free 3,000-hour curriculum

To update a class attribute, we use this syntax:

<class_name>.<attribute> = <value>

For example:

>>> class Dog:

kingdom = "Animalia"

def __init__(self, name, age):


self.name = name
self.age = age

>>> Dog.kingdom
'Animalia'

>>> Dog.kingdom = "New Kingdom"

>>> Dog.kingdom
'New Kingdom'

How to Delete a Class Attribute


We use del to delete a class attribute. For example:

>>> class Dog:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 117/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

kingdom = "Animalia"
Forum Donate
def __init__(self, name, age):
Learn to code — free 3,000-hour curriculum
self.name = name
self.age = age

>>> Dog.kingdom
'Animalia'

# Delete class attribute


>>> del Dog.kingdom

>>> Dog.kingdom
Traceback (most recent call last):
File "<pyshell#88>", line 1, in <module>

Dog.kingdom
AttributeError: type object 'Dog' has no attribute 'kingdom'

How to Define Methods


Methods represent the functionality of the instances of the class.

💡 Tip: Instance methods can work with the attributes of the instance
that is calling the method if we write self.<attribute> in the method
de nition.

This is the basic syntax of a method in a class. They are usually located
below __init__ :

class <ClassName>:

# Class attributes

# __init__

def <method_name>(self, <param1>, ...):


<code>
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 118/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
<code>
Forum Donate

Learn to code — free 3,000-hour curriculum


They may have zero, one, or more parameters if needed (just like
functions!) but instance methods must always have self as the rst
parameter.

For example, here is a bark method with no parameters (in addition


to self ):

class Dog:

def __init__(self, name, age):


self.name = name
self.age = age

def bark(self):
print(f"woof-woof. I'm {self.name}")

To call this method, we use this syntax:

<object_variable>.<method>(<arguments>)

For example:

# Create the instance


>>> my_dog = Dog("Nora", 10)

# Call the method


>>> my_dog.bark()
woof-woof. I'm Nora

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 119/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
Here we have aLearn
Player class—with
to code an increment_speed
free 3,000-hour curriculum method with
one parameter:

class Player:

def __init__(self, name):


self.name = name
self.speed = 50

def increment_speed(self, value):


self.speed += value

To call the method:

# Create instance
>>> my_player = Player("Nora")

# Check initial speed to see the change


>>> my_player.speed
50

# Increment the speed


>>> my_player.increment_speed(5)

# Confirm the change


>>> my_player.speed
55

💡 Tip: to add more parameters, just separate them with a comma. It is


recommended to add a space after the comma.

P i G dS i P h
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 120/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Properties, Getters and Setters in Python


Forum Donate
Getters and setters are methods that we can de ne to get and set the
Learn to code — free 3,000-hour curriculum
value of an instance attribute, respectively. They work as
intermediaries to "protect" the attributes from direct changes.

In Python, we typically use properties instead of getters and setters.


Let's see how we can use them.

To de ne a property, we write a method with this syntax:

@property
def <property_name>(self):
return self.<attribute>

This method will act as a getter, so it will be called when we try to


access the value of the attribute.

Now, we may also want to de ne a setter:

@<property_name>.setter
def <property_name>(self, <param>):
self.<attribute> = <param>

And a deleter to delete the attribute:

@<property_name>.deleter
def <property_name>(self):
del self.<attribute>

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 121/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
💡 Tip: you can write any code that you need in these methods to get,
Learn to code — free 3,000-hour curriculum
set, and delete an attribute. It is recommended to keep them as simple
as possible.

This is an example:

class Dog:

def __init__(self, name):


self._name = name

@property

def name(self):
return self._name

@name.setter
def name(self, new_name):
self._name = new_name

@name.deleter
def name(self):
del self._name

If we add descriptive print statements, we can see that they are called
when we perform their operation:

>>> class Dog:

def __init__(self, name):


self._name = name

@property
def name(self):
print("Calling getter")
return self._name
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 122/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

@name.setter
Forum Donate
def name(self, new_name):
Learn to code — free 3,000-hour curriculum
print("Calling setter")
self._name = new_name

@name.deleter
def name(self):
print("Calling deleter")
del self._name

>>> my_dog = Dog("Nora")

>>> my_dog.name
Calling getter
'Nora'

>>> my_dog.name = "Norita"


Calling setter

>>> my_dog.name
Calling getter
'Norita'

>>> del my_dog.name


Calling deleter

🔹 How to Work with Files in


Python
Working with les is very important to create powerful programs.
Let's see how you can do this in Python.

How to Read Files in Python


In Python, it's recommended to use a with statement to work with
les because it opens them only while we need them and it closes
them automatically when the process is completed.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 123/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
To read a le, we use this syntax:
Learn to code — free 3,000-hour curriculum

with open("<file_path>") as <file_var>:


<code>

We can also specify that we want to open the le in read mode with an
"r" :

with open("<file_path>", "r") as <file_var>:


<code>

But this is already the default mode to open a le, so we can omit it
like in the rst example.

This is an example:

with open("famous_quotes.txt") as file:


for line in file:
print(line)

or...

with open("famous_quotes.txt", "r") as file:


for line in file:
print(line)

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 124/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

💡 Tip: that's right! We can iterate over the lines of the Forum
le using a forDonate
loop. The le path cantobe
Learn relative
code — freeto the Python
3,000-hour script that we are
curriculum
running or it can be an absolute path.

How to Write to a File in Python


There are two ways to write to a le. You can either replace the entire
content of the le before adding the new content, or append to the
existing content.

with open("<file_path>", "w") as <file_var>:


<code>

To replace the content completely, we use the "w" mode, so we pass


this string as the second argument to open() . We call the .write()
method on the le object passing the content that we want to write as
argument.

For example:

words = ["Amazing", "Green", "Python", "Code"]

with open("famous_quotes.txt", "w") as file:


for word in words:
file.write(word + "\n")

When you run the program, a new le will be created if it doesn't exist
already in the path that we speci ed.

This will be the content of the le:


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 125/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Learn to code — free 3,000-hour curriculum


Amazing
Green
Python
Code

How to Append to a File in Python


However, if you want to append the content, then you need to use the
"a" mode:

with open("<file_path>", "a") as <file_var>:


<code>

For example:

words = ["Amazing", "Green", "Python", "Code"]

with open("famous_quotes.txt", "a") as file:


for word in words:
file.write(word + "\n")

This small change will keep the existing content of the le and it will
add the new content to the end.

If we run the program again, these strings will be added to the end of
the le:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 126/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Amazing Forum Donate


Green
Python Learn to code — free 3,000-hour curriculum
Code
Amazing
Green
Python
Code

How to Delete a File in Python


To delete a le with our script, we can use the os module. It is
recommended to check with a conditional if the le exists before
calling the remove() function from this module:

import os

if os.path.exists("<file_path>"):
os.remove("<file_path>")
else:
<code>

For example:

import os

if os.path.exists("famous_quotes.txt"):
os.remove("famous_quotes.txt")
else:
print("This file doesn't exist")

You might have noticed the rst line that says import os . This is an
import statement. Let's see why they are helpful and how you can
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 127/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

work with them. Forum Donate

Learn to code — free 3,000-hour curriculum

🔸 Import Statements in Python


Organizing your code into multiple les as your program grows in size
and complexity is good practice. But we need to nd a way to combine
these les to make the program work correctly, and that is exactly
what import statements do.

By writing an import statement, we can import a module (a le that


contains Python de nitions and statements) into another le.

These are various alternatives for import statements:

First Alternative:

import <module_name>

For example:

import math

💡 Tip: math is a built-in Python module.

If we use this import statement, we will need to add the name of the
module before the name of the function or element that we are
referring to in our code:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 128/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> import math Forum Donate


>>> math.sqrt(25)
Learn to code — free 3,000-hour curriculum
5.0

We explicitly mention in our code the module that the element


belongs to.

Second Alternative:

import <module> as <new_name>

For example:

import math as m

In our code, we can use the new name that we assigned instead of the
original name of the module:

>>> import math as m


>>> m.sqrt(25)
5.0

Third Alternative:

from <module_name> import <element>

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 129/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
For example: Learn to code — free 3,000-hour curriculum

from math import sqrt

With this import statement, we can call the function directly without
speci ying the name of the module:

>>> from math import sqrt

>>> sqrt(25)
5.0

Fourth Alternative:

from <module_name> import <element> as <new_name>

For example:

from math import sqrt as square_root

With this import statement, we can assign a new name to the element
imported from the module:

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 130/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

>>> from math import sqrt as square_root Forum Donate


>>> square_root(25)
5.0 Learn to code — free 3,000-hour curriculum

Fifth Alternative:

from <module_name> import *

This statement imports all the elements of the module and you can
refer to them directly by their name without specifying the name of
the module.

For example:

>>> from math import *

>>> sqrt(25)
5.0

>>> factorial(5)
120

>>> floor(4.6)
4

>>> gcd(5, 8)
1

💡 Tip: this type of import statement can make it more dif cult for us
to know which elements belong to which module, particularly when
we are importing elements from multiple modules.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 131/137
5/4/2021
p g p
Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
According to the Style Guide for Python Code:
Learn to code — free 3,000-hour curriculum

Wildcard imports (from <module> import *) should be


avoided, as they make it unclear which names are present in
the namespace, confusing both readers and many automated
tools.

🔹 List and Dictionary


Comprehension in Python
A really nice feature of Python that you should know about is list and
dictionary comprehension. This is just a way to create lists and
dictionaries more compactly.

List Comprehension in Python


The syntax used to de ne list comprehensions usually follows one of
these four patterns:

[<value_to_include> for <var> in <sequence>]

[<value_to_include> for <var1> in <sequence1> for <var2> in <sequen

[<value_to_include> for <var> in <sequence> if <condition>]

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 132/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

[<value> for Learn to in


<var1> code — free 3,000-hour
<sequence1> curriculum
for <var2> in <sequence2> if <co

💡 Tip: you should only use them when they do not make your code
more dif cult to read and understand.

Here we have some examples:

>>> [i for i in range(4, 15)]


[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

>>> [chr(i) for i in range(67, 80)]


['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']

>>> [i**3 for i in range(2, 5)]


[8, 27, 64]

>>> [i + j for i in range(5, 8) for j in range(3, 6)]


[8, 9, 10, 9, 10, 11, 10, 11, 12]

>>> [k for k in range(3, 35) if k % 2 == 0]


[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34]

>>> [i * j for i in range(2, 6) for j in range(3, 7) if i % j == 0]


[9, 16, 25]

List Comprehensions vs. Generator


Expressions in Python
List comprehensions are de ned with square brackets [] . This is
different from generator expressions, which are de ned with
parentheses () . They look similar but they are quite different. Let's
see why.
https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 133/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate
List comprehensions generate the entire sequence at once and
Learn to code — free 3,000-hour curriculum
store it in memory.

Generator expressions yield the elements one at a time when


they are requested.

We can check this with the sys module. In the example below, you
can see that their size in memory is very different:

>>> import sys


>>> sys.getsizeof([i for i in range(500)])
2132

>>> sys.getsizeof((i for i in range(500)))


56

We can use generator expressions to iterate in a for loop and get the
elements one at a time. But if we need to store the elements in a list,
then we should use list comprehension.

Dictionary Comprehension in Python


Now let's dive into dictionary comprehension. The basic syntax that
we need to use to de ne a dictionary comprehension is:

{<key_value>: <value> for <var> in <sequence>}

{<key_value>: <value> for <var> in <sequence> if <condition>}

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 134/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Forum Donate

Here we have some


Learnexamples
to code — of dictionary
free comprehension:
3,000-hour curriculum

>>> {num: num**3 for num in range(3, 15)}


{3: 27, 4: 64, 5: 125, 6: 216, 7: 343, 8: 512, 9: 729, 10: 1000, 11

>>> {x: x + y for x in range(4, 8) for y in range(3, 7)}


{4: 10, 5: 11, 6: 12, 7: 13}

This is an example with a conditional where we take an existing


dictionary and create a new dictionary with only the students who
earned a passing grade greater than or equal to 60:

>>> grades = {"Nora": 78, "Gino": 100, "Talina": 56, "Elizabeth": 4

>>> approved_students = {student: grade for (student, grade) in gra

>>> approved_students
{'Nora': 78, 'Gino': 100, 'Lulu': 67}

I really hope you liked this article and found it helpful. Now you know
how to write and work with the most important elements of Python.

⭐ Subscribe to my YouTube channel and follow me on Twitter to nd


more coding tutorials and tips. Check out my online course Python
Exercises for Beginners: Solve 100+ Coding Challenges

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 135/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners

Estefania Cassingena
Computer Science Navone
and Mathematics Forum
Student | Udemy Instructor | Author atDonate
freeCodeCamp News
Learn to code — free 3,000-hour curriculum

If you read this far, tweet to the author to show them you care.
Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has


helped more than 40,000 people get jobs as developers.
Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonpro t organization (United States

Federal Tax Identi cation Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of
videos, articles, and interactive coding lessons - all freely available to the public. We also have
thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives and help pay for servers,
services, and staff.

You can make a tax-deductible donation here.

Trending Guides

10 to the Power of 0 Recursion

Git Reset to Remote ISO File

R Value in Statistics ADB

What is Economics? MBR VS GPT

Module Exports Debounce

Python VS JavaScript Helm Chart


https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 136/137
5/4/2021 Python Code Examples – Sample Script Coding Tutorial for Beginners
y p

Model View Controller 80-20 Rule Forum Donate

React Testing Library Learn to code — free 3,000-hour


OSI Modelcurriculum

ASCII Table Chart HTML Link Code

Data Validation SDLC

Inductive VS Deductive JavaScript Keycode List

JavaScript Empty Array JavaScript Reverse Array

Best Instagram Post Time How to Screenshot on Mac

Garbage Collection in Java How to Reverse Image Search

Auto-Numbering in Excel Ternary Operator JavaScript

Our Nonpro t

About Alumni Network Open Source Shop Support Sponsors Academic Honesty

Code of Conduct Privacy Policy Terms of Service Copyright Policy

https://www.freecodecamp.org/news/python-code-examples-sample-script-coding-tutorial-for-beginners/#-how-to-work-with-files-in-python 137/137

You might also like