0% found this document useful (0 votes)
2 views9 pages

Beginning of Python #1

Python is a high-level, interpreted programming language known for its readability and simplicity, making it suitable for both beginners and experienced developers. It features a strong community and a rich ecosystem of libraries like NumPy and Pandas for data manipulation and analysis. The document also covers setting up a Python environment using Visual Studio Code, basic data types, and fundamental programming concepts such as functions, loops, and conditionals.

Uploaded by

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

Beginning of Python #1

Python is a high-level, interpreted programming language known for its readability and simplicity, making it suitable for both beginners and experienced developers. It features a strong community and a rich ecosystem of libraries like NumPy and Pandas for data manipulation and analysis. The document also covers setting up a Python environment using Visual Studio Code, basic data types, and fundamental programming concepts such as functions, loops, and conditionals.

Uploaded by

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

Python is a high-level, interpreted language with a syntax that emphasizes readability and

simplicity. This makes it an excellent choice for beginners, as well as for experienced
developers looking for a powerful and flexible language for their projects.

One of the key strengths of Python is its use of whitespace to delineate blocks of code. This
makes the code easier to read and understand, and helps to prevent common programming
errors such as incorrect indentation. For example, here's a simple Python function that prints the
message "Hello, world!" to the console:

def say_hello():
print("Hello, world!")

say_hello()
In this example, the def keyword is used to define a new function called say_hello, and the print
function is used to output the message to the console. The body of the function is indented to
show that it belongs to the say_hello function. This simple notation makes the code easy to read
and understand.

Python is also known for its vast and active community of developers, who have created a wide
variety of libraries and frameworks that make it easy to solve complex problems. For example,
NumPy is a popular library for working with arrays and numerical data, while Pandas is a
powerful library for data analysis and manipulation.

Here's an example that shows how easy it is to perform basic data analysis with Pandas. In this
example, we'll create a small dataset of sales figures and then use Pandas to analyze the data:

import pandas as pd

# Create a Pandas dataframe from a dictionary


sales_data = {
'date': ['2022-01-01', '2022-01-02', '2022-01-03'],
'units_sold': [100, 120, 115]
}

sales_df = pd.DataFrame(sales_data)

# Calculate the total sales for each day


sales_df['total_sales'] = sales_df['units_sold'] * 100

# Calculate the average sales per day


average_sales = sales_df['total_sales'].mean()
print(f"The average sales per day is: ${average_sales:.2f}")
In this example, we first import the Pandas library and create a dictionary called sales_data to
hold our data. We then use the pd.DataFrame() function to create a Pandas dataframe from the
sales_data dictionary.

Next, we calculate the total sales for each day by creating a new column called total_sales and
assigning it the product of the units_sold and 100 (assuming each unit is sold for $100). We
then use the mean() function to calculate the average sales per day and print the result using
the f-string notation.

This is just a taste of what's possible with Python and its extensive collection of libraries and
frameworks. Whether you're building web applications, data pipelines, machine learning models,
or just exploring the world of programming for the first time, Python is an excellent choice that
offers powerful features, a supportive community, and a bright future.

So, what are you waiting for? Dive into Python programming and see where this exciting
language takes you.

We start by setting up our Python environment, which involves installing Python and setting up a
code editor. The video recommends using Visual Studio Code as our code editor.

First, let's install Python. We can download the latest version of Python from the official website:
https://www.python.org/downloads/. The video recommends installing Python 3.9.2. Once we've
downloaded the installer, we can run it and follow the prompts to install Python. Be sure to
check the box that says "Add Python to PATH" during the installation process.

Next, let's set up Visual Studio Code. We can download it from the official website:
https://code.visualstudio.com/. Once we've downloaded the installer, we can run it and follow the
prompts to install Visual Studio Code.

Once we have both Python and Visual Studio Code installed, we can start setting up our Python
environment in Visual Studio Code. We can do this by installing the Python extension for Visual
Studio Code. We can find this extension by going to the Extensions view in Visual Studio Code
(you can open this view by clicking on the square icon with a plus sign in the Activity Bar on the
side of the window). Once we've installed the Python extension, we can open a new Python file
by going to File > New File, and then saving the file with a .py extension.

The video shows how to use Visual Studio Code to run Python code. We can do this by opening
a Python file, and then clicking on the "Run Python File in Terminal" button in the top right corner
of the window. This will open a new terminal window at the bottom of the screen, and run our
Python code in that window.

The video also shows how to use Visual Studio Code to debug Python code. We can do this by
setting breakpoints in our code (by clicking in the gutter next to the line number), and then
clicking on the "Start Debugging" button in the top toolbar. This will open a new Debug Console
window, and allow us to step through our code line by line.

One of the benefits of using Visual Studio Code as our code editor is that it has built-in support
for version control with Git. This means that we can use Visual Studio Code to commit our code
to a Git repository, and then push our changes to a remote repository (such as GitHub). The
video shows how to do this by creating a new Git repository in Visual Studio Code, and then
committing and pushing our changes to GitHub.

Here's an example of some Python code that we can write and run in Visual Studio Code:

# This is a comment

# This is a variable
my_variable = 10

# This is a string
my_string = "Hello, world!"

# This is a list
my_list = [1, 2, 3, 4, 5]

# This is a dictionary
my_dict = {"name": "Alice", "age": 30}

# This is a for loop


for i in my_list:
print(i)

# This is a function
def my_function(x):
return x * 2

# This is a call to the function


print(my_function(5))
When we run this code in Visual Studio Code, it will print the numbers 1 through 5 to the
terminal window, and then print 10 to the terminal window (because my_function(5) returns 5 *
2, which is 10).

Here's a hand-drawn plot of the function my_function(x):

|
|
| .
| .
| .
|.
-----+-----------------
0 2 4 6 8 10
In this chapter introduction for beginners, we'll explore the basics of this versatile and
widely-used programming language.

Python is a high-level, interpreted language with a syntax that emphasizes readability and
simplicity. This makes it an excellent choice for beginners, as well as for experienced
developers looking for a powerful and flexible language for their projects.

One of the key strengths of Python is its use of whitespace to delineate blocks of code. This
makes the code easier to read and understand, and helps to prevent common programming
errors such as incorrect indentation. For example, here's a simple Python function that prints the
message "Hello, world!" to the console:

def say_hello():
print("Hello, world!")

say_hello()
In this example, the def keyword is used to define a new function called say_hello, and the print
function is used to output the message to the console. The body of the function is indented to
show that it belongs to the say_hello function. This simple notation makes the code easy to read
and understand.

Python is also known for its vast and active community of developers, who have created a wide
variety of libraries and frameworks that make it easy to solve complex problems. For example,
NumPy is a popular library for working with arrays and numerical data, while Pandas is a
powerful library for data analysis and manipulation.

Here's an example that shows how easy it is to perform basic data analysis with Pandas. In this
example, we'll create a small dataset of sales figures and then use Pandas to analyze the data:

import pandas as pd

# Create a Pandas dataframe from a dictionary


sales_data = {
'date': ['2022-01-01', '2022-01-02', '2022-01-03'],
'units_sold': [100, 120, 115]
}

sales_df = pd.DataFrame(sales_data)
# Calculate the total sales for each day
sales_df['total_sales'] = sales_df['units_sold'] * 100

# Calculate the average sales per day


average_sales = sales_df['total_sales'].mean()

print(f"The average sales per day is: ${average_sales:.2f}")


In this example, we first import the Pandas library and create a dictionary called sales_data to
hold our data. We then use the pd.DataFrame() function to create a Pandas dataframe from the
sales_data dictionary.

Next, we calculate the total sales for each day by creating a new column called total_sales and
assigning it the product of the units_sold and 100 (assuming each unit is sold for $100). We
then use the mean() function to calculate the average sales per day and print the result using
the f-string notation.

This is just a taste of what's possible with Python and its extensive collection of libraries and
frameworks. Whether you're building web applications, data pipelines, machine learning models,
or just exploring the world of programming for the first time, Python is an excellent choice that
offers powerful features, a supportive community, and a bright future.

So, what are you waiting for? Dive into Python programming and see where this exciting
language takes you!

Numbers in Python are used to represent numerical values. Python


supports various types of numbers including Integers, Floats, and
Complex numbers. Here’s an example:

# Integers
x = 10
print(x)

# Floats
y = 20.5
print(y)

# Complex numbers
z = 1j
print(z)

In the video, the instructor explained the difference between integers


and floats with an anecdote. She mentioned that integers are whole
numbers, for example, 5, 10, or 15. Floats, on the other hand, are
numbers with a decimal point, for example, 5.5, 10.2, or 15.8.

Next, let’s talk about Strings. Strings in Python are used to represent
textual data. They are surrounded by either single quotes (') or double
quotes ("). Here’s an example:

# Single quotes
name = 'John'
print(name)

# Double quotes
address = "123 Main St."
print(address)

In the video, the instructor demonstrated how to concatenate two


strings using the + operator. Here’s an example:

first_name = 'John'
last_name = 'Doe'

full_name = first_name + ' ' + last_name


print(full_name)

The output of the code above will be John Doe.

Finally, let’s explore Booleans in Python. Booleans are used to


represent logical values, either True or False. Here’s an example:

# Boolean value
is_student = True
print(is_student)

In the video, the instructor used a step-by-step calculation to explain


how to use Boolean values in a conditional statement. Here’s an
example:

# Boolean value
is_student = True

# Conditional statement
if is_student:
print('The person is a student.')
else:
print('The person is not a student.')

The output of the code above will be The person is a student..

In conclusion, this chapter covered the basics of data types in Python,


including Numbers, Strings, and Booleans. Understanding these data
types is crucial when working with Python, as they are the building
blocks for more complex data structures.

Type Conversion and Manipulation in


Python
Arithmetic Operators in Python
●​ Addition: +
●​ Subtraction: -
●​ Multiplication: *
●​ Division: /
●​ Modulus: %
●​ Exponentiation: **
●​ Floor division: //

Logical Operators for Complex Conditions


●​ and: Returns True if both statements are true
●​ or: Returns True if at least one of the statements is true
●​ not: Reverse the result, returns False if the result is true
Looping with While Loops in Python
●​ Used for repeated execution based on a condition
Getting User Input and Performing Basic Calculations
●​ Use the input() function to get user input
●​ Convert input to desired data type using functions like int(),
float()

Getting the Length of a List with the Len Function


●​ len(list) returns the number of elements in a list

Working with Lists in Python: Methods and Operations


●​ Access elements with index
●​ Change element values by referring to the index
●​ Add elements with append() or insert()
●​ Remove elements with remove(), pop(), or del

Tuples in Python: Immutable Sequences of Objects


●​ Similar to lists, but cannot be changed after creation
Understanding Comparison Operators in Python
●​ Equal: ==
●​ Not equal: !=
●​ Greater than: >
●​ Less than: <
●​ Greater than or equal to: >=
●​ Less than or equal to: <=

Decision Making with If Statements in Python


●​ Used for decision making operations based on conditions
Understanding Primitive and Complex Data Types in
Python
●​ Primitive: Numbers, Strings, Booleans
●​ Complex: Lists, Tuples, Sets, Dictionaries
Type Conversion and Manipulation in Python
●​ Convert between data types with functions like int(),
float(), str(), bool()

Using the For Loop to Iterate Over Lists in Python


●​ Use for loop and range() function to iterate through list
elements
Understanding the Range Function in Python
●​ Generates a sequence of numbers that can be used in for
loops
Checking for Existence of Items in Lists with the In
Operator
●​ Use the in operator to check if an item exists in a list

You might also like