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

Presentation 1

Uploaded by

bnhatm216
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)
3 views

Presentation 1

Uploaded by

bnhatm216
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/ 28

Python Introduction

Python is a popular programming language

What is Python?

• web development (server-side),

It is used for: • software development,


• mathematics,
• system scripting.
Python Introduction
Python is a popular programming language

Why Python?

• Python has syntax that • Python works on • Python has a simple


allows developers to write different platforms syntax similar to the
programs with fewer lines (Windows, Mac, English language.
than some other Linux, Raspberry Pi,
programming languages. etc).
Python Introduction

Python is a popular programming language


Why Python?

• Cross-platform
• Data analysis
• AI
• Jobs and Salaries
Python Introduction

Python is a popular programming language


Why Python?

• Python can be treated • Python runs on an interpreter


in a procedural way, system, meaning that code can
an object-oriented be executed as soon as it is
way or a functional written. This means that
way. prototyping can be very quick.
Python Introduction

Python Syntax compared to other programming languages

• Python was designed for readability, and has some similarities to the English language with

influence from mathematics.

• Python uses new lines to complete a command, as opposed to other programming languages

which often use semicolons or parentheses.

• Python relies on indentation, using whitespace, to define scope; such as the scope of loops,

functions and classes. Other programming languages often use curly-brackets for this purpose.

print("Hello,\nWorld!")
Hello,
World!
Python Introduction
Python Getting Started

Python Install
Python Quickstart

Python is an interpreted programming language, this means that as a


developer you write Python (.py) files in a text editor and then put those files
into the python interpreter to be executed.
Python Introduction
Methods to Run a Script in Python

There are various methods to Run a Python script

• Interactive Mode
This is the most frequently used way to
• Command Line
1. Interactive Mode run a Python code. Here basically we do
• Text Editor (VS Code)
the following:
• IDE (PyCharm)

1. Open command prompt / terminal in your system C:\Users\Deepa>python


Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19
2. Enter the command - python or python3 (based on python
2021, 13:44:55) [MSC v.1928 64 bit
version installed in your system)
(AMD64)] on win32 Type "help",
3. If we see these : >>> then, we are into our python prompt. It will
"copyright", "credits" or "license" for
look something like this: more information. >>>
Python Introduction
Methods to Run a Script in Python

There are various methods to Run a Python script

Pros:
1. Best for testing every single line of code written.
• Interactive Mode
2. In this approach, each line of code is evaluated and executed immediately as soon as we hit ↵ Enter.
3. Great development tool for experimentation of Python code on the way.

Cons:
1. The code is gone as soon as we close the terminal(or the interactive session)
2. Not user friendly way to write long code.
3. To close the interactive session we can:
• Type quit() or exit()
• Press ctrl + z and hit ↵ Enter for windows.
Python Introduction
Methods to Run a Script in Python

There are various methods to Run a Python script

Running Python scripts on Windows via the Using Script Filename


• Command Line command line provides a direct and
python hello.py
efficient way to execute code

Redirecting output

python hello.py > output .txt


Python Introduction
Methods to Run a Script in Python

There are various methods to Run a Python script

To run Python script on a text editor like VS Code (Visual Studio Code) then you will have
• a Text Editor to do the following:

• Go to the extension section or press ‘Ctrl+Shift+X’ on Windows, then search and install the extension named ‘Python’
and ‘Code Runner’. Restart your vs code after that.
• Now, create a new file with the name ‘hello.py’ and write the below code in it: print('Hello World!')
Python Introduction
Methods to Run a Script in Python

There are various methods to Run a Python script

To run Python script on an IDE (Integrated Development Environment) like PyCharm, you
• using an IDE will have to do the following:

• Create a new project.


• Give a name to that project as ‘GfG’ and click on Create.
• Select the root directory with the project name we specified in the last step. Right-click on it, go to New, anto, and click
on the ‘Python file’ option. Then give the name of the file as ‘hello’ (you can specify any name as per your project
requirement). This will create a ‘hello.py’ file in the project root directory.
Python Introduction
Python Syntax

Execute Python Syntax

As we learned in the previous page, Python syntax can be executed by different ways

Example
C:\Users\Your Name>python myfile.py

Python Indentation

Indentation refers to the spaces at the beginning of a code line.


Python uses indentation to indicate a block of code.

Example
if 5 > 2:
if 5 > 2:
Python will give you an print("Five is greater than two!")
error if you skip the print("Five is greater than two!") if 5 > 2:
indentation: print("Five is greater than two!")
Python Introduction

Python Syntax

Python Indentation

You have to use the same number of spaces in the same block of code,
otherwise Python will give you an error:

Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Introduction

Python Comments

• Comments can be used to explain Python code.


• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.

Example
#This is a comment
print("Hello, World!")
print("Hello, World!") #This is a comment
#print("Hello, World!")
print("Cheers, Mate!")
Python Introduction

Python Comments
Multiline Comments

#This is a comment """


This is a comment
#written in written in
#more than just one line more than just one line
"""
print("Hello, World!") print("Hello, World!")
Python Introduction

Python Variables
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 Introduction

Python Variables
Variables

Variables do not need to be declared with any particular type, and can even
change type after they have been set.

Example x = 4 # x is of type int


x = "Sally" # x is now of type str
print(x)
Python Introduction

Python Variables
Casting
If you want to specify the data type of a variable, this can be done with casting.
Example
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0

Get the Type


You can get the data type of a variable with the type() function.

Example
x = 5
y = "John"
print(type(x))
print(type(y))
Python Introduction

Python Variables
Single or Double Quotes?

String variables can be declared either by using single or double quotes:

Example x = "John"
# is the same as
x = 'John'

Case-Sensitive

Variable names are case-sensitive.

Example
a = 4
A = "Sally"
#A will not overwrite a
Python Introduction
Python Variable
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 must start with a letter or the underscore character
• A variable name cannot start with a number
• 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)
• A variable name cannot be any of the Python keywords.
Example
myvar = "John" Example
my_var = "John" Illegal variable names:
_my_var = "John" 2myvar = "John"
myVar = "John" my-var = "John"
MYVAR = "John" my var = "John"
myvar2 = "John"
Python Introduction
Python Variable
Multi Words Variable Names
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"
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 Introduction
Python Variable
Assign Multiple Values
Python allows you to assign values to multiple variables in one line:
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)

One Value to Multiple Variables


Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Python Introduction
Python Variable
Assign Multiple Values
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
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Python Introduction
Python Variable
Output Variables
The Python print() function is often used to output variables.

Example
x = "Python is awesome"
print(x)

In the print() function, you output multiple variables, separated by a comma:

Example
x = "Python" x = "Python " x = 5 x = 5
y = "is" y = "is " y = 10 y = "John"
z = "awesome" z = "awesome" print(x + y) print(x + y)
print(x, y, z) print(x + y + z) Try it Yourself »
Python Introduction
Python Variable
Global Variables
Create a variable outside of a function, and use it inside the function

Example x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()

Create a variable inside a function, with the same name as the global variable

Example
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()

print("Python is " + x)
Python Introduction
Python Variable
The global Keyword
If you use the global keyword, the variable belongs to the global scope:
def myfunc():
global x
Example
x = "fantastic"
myfunc()
print("Python is " + x)

To change the value of a global variable inside a function, refer to the variable by using the global keyword:

Example x = "awesome"
def myfunc():
global x
x = "fantastic"

myfunc()

print("Python is " + x)
Python Introduction

Python Data Types

Built-in Data Types


Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric Types: int, float, complex

Sequence list, tuple, range


Types:
Mapping Type: dict
Set Types: set, frozenset
Boolean bool
Type:
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
Python Introduction

Python Data Types

Built-in Data Types

x = "Hello World" str x = {"apple", set


x = 20 int "banana", "cherry"}
x = 20.5 float x = frozenset
frozenset({"apple",
x = 1j complex "banana", "cherry"})
x = ["apple", "banana", list x = True bool
"cherry"]
x = b"Hello" bytes
x = ("apple", "banana", tuple
"cherry") x = bytearray(5) bytearray
x = range(6) range x = memoryview
memoryview(bytes(5))
x = {"name" : "John", dict
"age" : 36} x = None

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy