Chapter 1
Chapter 1
Chapter 1
What is Python?
• There are two ways you can execute your Python program:
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
• Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.
• Python can be treated in a procedural way, or an object-oriented way or a functional way.
1. Web Development: Python is used to build web applications using frameworks like
Django, Flask, and Pyramid. These frameworks provide tools and libraries for handling
web requests, managing databases, and more.
2. Data Science and Machine Learning: Python is popular in data science and machine
learning due to libraries like NumPy, pandas, Matplotlib, and scikit-learn. These
libraries provide tools for data manipulation, analysis, visualization, and machine
learning algorithms.
5. Desktop Applications: Python can be used to build desktop applications using libraries
like Tkinter, PyQt, and wxPython. These libraries provide tools for creating graphical user
interfaces (GUIs), handling user input, and more.
6. Scripting and Automation: Python is commonly used for scripting and automation tasks
due to its simplicity and readability. It can be used to automate repetitive tasks, manage
files and directories, and more.
7. Web Scraping and Crawling: Python is widely used for web scraping and crawling using libraries like Beautiful Soup
and Scrapy. These libraries provide tools for extracting data from websites, parsing HTML and XML, and more.
8. Education and Research: Python is commonly used in education and research due to its simplicity and readability.
Many universities and research institutions use Python for teaching programming and conducting research in various
fields.
9. Community and Ecosystem: Python has a large and active community, which contributes to its ecosystem. There are
many third-party libraries and frameworks available for various purposes, making Python a versatile language for
many applications.
10. Cross-Platform: Python is a cross-platform language, which means that Python code can run on different operating
systems without modification. This makes it easy to develop and deploy Python applications on different platforms.
Python
• Python syntax can be executed by writing directly in the
Command Line
• print("Hello, World!")
• x = "Python"
• y = "is"
• z = "awesome"
• print(x, y, z)
Example:
if 5 > 2:
Example:
if 5 > 2:
• Comments can be placed at the end of a line, and Python will ignore the rest of the line:
• Example
• A comment does not have to be text that explains the code, it can also be used to prevent Python from
executing code:
• Example
#print("Hello, World!")
print("Cheers, Mate!")
Python Comments
• Multiline Comments
• Example
• """
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python Variables
• Variables are containers for storing data values.
• Creating Variables
• Python has no command for declaring a variable.
• A variable is created the moment you first assign
a value to it.
• Example:
x=5
y = "John"
print(x)
print(y)
Python • Variables do not need to be • Casting
• Example
x=5
y = "John"
print(type(x))
print(type(y))
Python Variables
• Example
x = "John"
print(x)
#double quotes are the same as single quotes:
x = 'John'
print(x)
Python Variables
• Case-Sensitive
• Variable names are case-sensitive.
• Example
• This will create two variables:
a=4
A = "Sally"
#A will not overwrite a
Python - Variable Names
• Variable Names.
• A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
• Example
• Legal variable names:
• myvar = "John" • print(myvar)
• my_var = "John" • print(my_var)
• _my_var = "John" • print(_my_var)
• myVar = "John" • print(myVar)
• MYVAR = "John" • print(MYVAR)
• myvar2 = "John" • print(myvar2)
Python - Variable Names
• Example
2myvar = "John"
my-var = "John"
my var = "John”
• Variable names with more than one word can be difficult to read.
• There are several techniques you can use to make them more readable:
• Camel Case
• Each word, except the first, starts with a capital letter:
• myVariableName = "John"
Python - Variable Names
• Pascal Case
• Each word starts with a capital letter:
• MyVariableName = "John"
• Snake Case
• Each word is separated by an underscore character:
• my_variable_name = "John"
Python Variables - Assign Multiple Values
• Note: Make sure the number of variables matches the number of values, or else
you will get an error.
One Value to Multiple Variables
• Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
• If you have a collection of values in a list, tuple etc. Python allows
you to extract the values into variables. This is called unpacking.
• Example
• Unpack a list:
x, y, z = fruits
print(x)
print(y)
print(z)
Python - Output Variables
• Output Variables
• Example:
x = "Python is awesome"
print(x)
Python - Output Variables
• Example:
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
• Notice the space character after "Python " and "is ", without them the result would be
"Pythonisawesome".
• For numbers, the + character works as a mathematical
operator:
• Example:
x=5
y = 10
Python - Output print(x + y)
Variables • In the print() function, when you try to combine a string and
a number with the + operator, Python will give you an error:
• Example:
x=5
y = "John"
print(x + y)
• The best way to output multiple variables in
the print() function is to separate them with
commas, which even support different data types:
Python - Output
Example:
Variables
x=5
y = "John"
print(x, y)
Python - Global Variables
• Variables that are created outside of a function (as in all of the examples in the previous pages) are known as global variables.
• Global variables can be used by everyone, both inside of functions and outside.
• Function: is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain
actions, and they are important for reusing code: Define the code once and use it many times.
• Example:
• Create a variable outside of a function and use it inside the function.
x = "awesome”
def myfunc():
print("Python is " + x)
myfunc()
Python - Global Variables
• If you create a variable with the same name inside a function, this variable
will be local, and can only be used inside the function. The global variable
with the same name will remain as it was, global and with the original value.
• Example Create a variable inside a function, with the same name as the
global variable
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
The global Keyword
• Normally, when you create a variable inside a function, that variable is
local, and can only be used inside that function.
• To create a global variable inside a function, you can use the global
keyword.
• Example:
• If you use the global keyword, the variable belongs to the global scope:
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
The global Keyword
• Also, use the global keyword if you want to change a global variable
inside a function.
• Example:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python Data Types
Data Types
• Variables can store data of different types, and different types can do different things.
• int
• float
• Complex
x = 1 # int
y = 2.8 # float
z = 1j # complex
Data Type: Int
• Int, or integer, is a whole number, positive or
negative, without decimals, of unlimited length.
• Example Integers:
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Data Type: Float
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Data Type: Complex
• Example Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
• You can convert from one type to another with the int(), float(), and complex() methods:
import random
print(random.randrange(1, 10))
End of Chapter 1