Presentation 1
Presentation 1
What is Python?
Why Python?
• Cross-platform
• Data analysis
• AI
• Jobs and Salaries
Python Introduction
• Python was designed for readability, and has some similarities to the English language with
• Python uses new lines to complete a command, as opposed to other programming languages
• 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
• 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)
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
Redirecting output
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
To run Python script on an IDE (Integrated Development Environment) like PyCharm, you
• using an IDE will have to do the following:
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
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
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
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.
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
Example
x = 5
y = "John"
print(type(x))
print(type(y))
Python Introduction
Python Variables
Single or Double Quotes?
Example x = "John"
# is the same as
x = 'John'
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)
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)
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