SlideShare a Scribd company logo
Python Basics
Eng.Elaf Ahmed Saeed
Python Introduction
What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
What can Python do?
• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
• Python can be used to handle big data and perform complex mathematics.
• Python can be used for rapid prototyping, or for production-ready software
development.
Why Python?
• 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, an object-oriented way or a functional way.
Python Install
Python Install
• Many PCs and Macs will have python already installed.
• To check if you have python installed on a Windows PC, search in the start bar
for Python or run the following on the Command Line (cmd.exe):
C:UsersYour Name>python –version
• If you find that you do not have python installed on your computer, then you
can download it for free from the following website: https://www.python.org/
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.
• The way to run a python file is like this on the command line:
C:UsersYour Name>python helloworld.py
• Where "helloworld.py" is the name of your python file.
Python Quickstart
• helloworld.py, which can be done in any text editor:
print("Hello, World!")
 result: Hello, World!
The Python Command Line
• To test a short amount of code in python sometimes it is quickest and easiest
not to write the code in a file. This is made possible because Python can be run
as a command line itself.
• Type the following on the Windows, Mac or Linux command line:
C:UsersYour Name>python
• Or, if the "python" command did not work, you can try "py":
C:UsersYour Name>py
The Python Command Line
• Example 1:
print("Hello, World!")
Result: Hello, World!
Python Comments
The Python Command Line
• 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.
The Python Command Line
• Single Line Comments
Comments starts with a #, and Python will ignore them:
Example1:
#This is a comment
print("Hello, World!")
Example2:
print("Hello, World!") #This is a comment
The Python Command Line
• Multi Line Comments
Example1:
#This is a comment
#written in
#more than just one line
print("Hello, World!")
The Python Command Line
• Multi Line Comments
Example2:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python Variables
Creating Variables
• Variables are containers for storing data values.
• Unlike other programming languages, 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)
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)
Variable Names
Example:
#Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
#Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Assign Value to Multiple Variables
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)
Assign Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output Variables
The Python print statement is often used to output variables.
To combine both text and a variable, Python uses the + character:
Example:
x = "awesome"
print("Python is " + x)
Output Variables
You can also use the + character to add a variable to another variable:
Example:
x = "Python is "
y = "awesome"
z = x + y
print(z)
Output Variables
For numbers, the + character works as a mathematical operator:
Example:
x = 5
y = 10
print(x + y)
Output Variables
If you try to combine a string and a number, Python will give you an error:
Example:
x = 5
y = "John"
print(x + y)
Global Variables
• Variables that are created outside of a function (as in all of the examples above) are known as global
variables.
• Global variables can be used by everyone, both inside of functions and outside.
Example
Create a variable outside of a function, and use it inside the function
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Global Variables
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)
Result:
Python is fantastic
Python is awesome
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)
Result:
Python is fantastic
The global Keyword
Example
To change the value of a global variable inside a function, refer to the variable by using the global
keyword:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Result:
Python is fantastic
Python Data Types
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Getting the Data Type
You can get the data type of any object by using the type() function:
Example
Print the data type of the variable x:
x = 5
print(type(x))
Result:
<class 'int'>
Python Numbers
Python Numbers
There are three numeric types in Python:
• int
• float
• Complex
Variables of numeric types are created when you assign a value to them:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
Python Numbers
To verify the type of any object in Python, use the type() function:
Example
Result:
<class 'int'>
<class 'float'>
<class 'complex'>
Python Numbers
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
Example
Result:
<class 'int'>
<class 'int'>
<class 'int'>
Python Numbers
Float
Float, or "floating point number" is a number, positive or negative, containing one or more
decimals.
Example
Result:
<class 'float'>
<class 'float'>
<class 'float'>
Python Numbers
Complex
Complex numbers are written with a "j" as the imaginary part:
Example
Result:
<class 'complex'>
<class 'complex'>
<class 'complex'>
Python Numbers
Type Conversion
You can convert from one type to another with the int(), float(), and complex() methods:
Example
Result:
1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
Python Numbers
Note: You cannot convert complex numbers into another number type.
Random Number
Python does not have a random() function to make a random number, but Python has a built-in
module called random that can be used to make random numbers:
Example
Result:
1.0
2
(1+0j)
<class 'float'>
<class 'int'>
<class 'complex'>
Python Numbers
Example
Result:
1
Eng. Elaf Ahmed Saeed
linkden: www.linkedin.com/in/elaf-a-saeed-97bbb6150
facebook: https://www.facebook.com/profile.php?id=100004305557442
github: https://github.com/ElafAhmedSaeed
youtube: https://youtube.com/channel/UCE_RiXkyqREUdLAiZcbBqSg
slideshare: https://www.slideshare.net/ElafASaeed
Slideplayer: https://slideplayer.com/slide/18029243/
Google Scholar:
https://scholar.google.com/citations?user=VIpVZKkAAAAJ&hl=ar&gmla=AJsN-
F7PIgAjWJ44Hzb18fwPqJaaUmG0XzbLdzx09

More Related Content

What's hot (20)

PPT
Flag registers (assembly language) with types and examples
Computer_ at_home
 
PPTX
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
PPT
Object and class relationships
Pooja mittal
 
PPTX
Introduction to Basics of Python
Elewayte
 
PPTX
Fundamentals of OOP (Object Oriented Programming)
MD Sulaiman
 
PPTX
Introduction to python
Ayshwarya Baburam
 
PDF
Constructors and destructors
Nilesh Dalvi
 
PPTX
Oop c++class(final).ppt
Alok Kumar
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPT
One Dimensional Array
dincyjain
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPT
Operator Overloading
Nilesh Dalvi
 
PPT
Memory allocation in c
Prabhu Govind
 
PPTX
Arithmetic Expression
Mahmud Hasan Tanvir
 
PPT
Primitive data types in java
Umamaheshwariv1
 
PPTX
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
PDF
Introduction to algorithms
subhashchandra197
 
ODP
Python Modules
Nitin Reddy Katkam
 
Flag registers (assembly language) with types and examples
Computer_ at_home
 
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Object and class relationships
Pooja mittal
 
Introduction to Basics of Python
Elewayte
 
Fundamentals of OOP (Object Oriented Programming)
MD Sulaiman
 
Introduction to python
Ayshwarya Baburam
 
Constructors and destructors
Nilesh Dalvi
 
Oop c++class(final).ppt
Alok Kumar
 
07. Virtual Functions
Haresh Jaiswal
 
One Dimensional Array
dincyjain
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Operator Overloading
Nilesh Dalvi
 
Memory allocation in c
Prabhu Govind
 
Arithmetic Expression
Mahmud Hasan Tanvir
 
Primitive data types in java
Umamaheshwariv1
 
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Introduction to algorithms
subhashchandra197
 
Python Modules
Nitin Reddy Katkam
 

Similar to Python basics_ part1 (20)

PPTX
python_class.pptx
chandankumar943868
 
PDF
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
manikamr074
 
PPTX
modul-python-part1.pptx
Yusuf Ayuba
 
PPTX
Basic concept of Python.pptx includes design tool, identifier, variables.
supriyasarkar38
 
PPTX
Introduction to python for the abs .pptx
Mark Musah Ibrahim
 
PDF
Python Programming
Saravanan T.M
 
PPTX
Python Programming-1.pptx of python by computer
sharanyarashmir5
 
PPTX
Learn about Python power point presentation
omsumukh85
 
PPTX
Python basics
ssuser4e32df
 
PDF
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
PPTX
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
PPTX
UNIT 1 .pptx
Prachi Gawande
 
PPTX
Chapter 1-Introduction and syntax of python programming.pptx
atharvdeshpande20
 
PPTX
python introduction initial lecture unit1.pptx
ChandraPrakash715640
 
PPTX
Chapter1 python introduction syntax general
ssuser77162c
 
PPTX
Python Basics
Adheetha O. V
 
PPTX
Python 01.pptx
AliMohammadAmiri
 
PDF
Intro-to-Python-Part-1-first-part-edition.pdf
ssuser543728
 
PDF
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
AbdulmalikAhmadLawan2
 
PPTX
python
ultragamer6
 
python_class.pptx
chandankumar943868
 
PART - 1 Python Introduction- Variables- Data types - Numeric- String- Boole...
manikamr074
 
modul-python-part1.pptx
Yusuf Ayuba
 
Basic concept of Python.pptx includes design tool, identifier, variables.
supriyasarkar38
 
Introduction to python for the abs .pptx
Mark Musah Ibrahim
 
Python Programming
Saravanan T.M
 
Python Programming-1.pptx of python by computer
sharanyarashmir5
 
Learn about Python power point presentation
omsumukh85
 
Python basics
ssuser4e32df
 
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
UNIT 1 .pptx
Prachi Gawande
 
Chapter 1-Introduction and syntax of python programming.pptx
atharvdeshpande20
 
python introduction initial lecture unit1.pptx
ChandraPrakash715640
 
Chapter1 python introduction syntax general
ssuser77162c
 
Python Basics
Adheetha O. V
 
Python 01.pptx
AliMohammadAmiri
 
Intro-to-Python-Part-1-first-part-edition.pdf
ssuser543728
 
CSC2308 - PRINCIPLE OF PROGRAMMING II.pdf
AbdulmalikAhmadLawan2
 
python
ultragamer6
 
Ad

More from Elaf A.Saeed (20)

PDF
IOT NodeMCU - NodeMCU Webserver
Elaf A.Saeed
 
PDF
IOT NodeMCU - Ubidots Platform to Turn on LEDs
Elaf A.Saeed
 
PDF
IOT NodeMCU - Thinger Platform to Turn on LEDs
Elaf A.Saeed
 
PDF
IOT NodeMCU - IFTTT Templet to send SMS
Elaf A.Saeed
 
PDF
Getting date and time from ntp server with esp8266 node mcu
Elaf A.Saeed
 
PDF
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LED
Elaf A.Saeed
 
PDF
IOT NodeMCU - NodeMCU Connection to Internet
Elaf A.Saeed
 
PDF
Lesson 10- NodeMCU with LCD I2C
Elaf A.Saeed
 
PDF
Lesson 9- NodeMCU with Arduino UNO (UART)
Elaf A.Saeed
 
PDF
Lesson 8- NodeMCU with Servo Motor
Elaf A.Saeed
 
PDF
Lesson 7- NodeMCU with DC Motor
Elaf A.Saeed
 
PDF
Lesson 6 - NodeMCU with PWM Pin
Elaf A.Saeed
 
PDF
lesson4 - NodeMCU control led
Elaf A.Saeed
 
PDF
lesson2 - Nodemcu course - NodeMCU dev Board
Elaf A.Saeed
 
PDF
lesson1 - Getting Started with ESP8266
Elaf A.Saeed
 
PDF
Embedded system course projects - Arduino Course
Elaf A.Saeed
 
PDF
Embedded system introduction - Arduino Course
Elaf A.Saeed
 
PDF
Pyton with rasperry pi
Elaf A.Saeed
 
PDF
Summary of MATLAB Functions-Part1
Elaf A.Saeed
 
PDF
MATLAB Basics-Part1
Elaf A.Saeed
 
IOT NodeMCU - NodeMCU Webserver
Elaf A.Saeed
 
IOT NodeMCU - Ubidots Platform to Turn on LEDs
Elaf A.Saeed
 
IOT NodeMCU - Thinger Platform to Turn on LEDs
Elaf A.Saeed
 
IOT NodeMCU - IFTTT Templet to send SMS
Elaf A.Saeed
 
Getting date and time from ntp server with esp8266 node mcu
Elaf A.Saeed
 
ESP8266 NodeMCU Server, Client, Station Mode, and Client Control LED
Elaf A.Saeed
 
IOT NodeMCU - NodeMCU Connection to Internet
Elaf A.Saeed
 
Lesson 10- NodeMCU with LCD I2C
Elaf A.Saeed
 
Lesson 9- NodeMCU with Arduino UNO (UART)
Elaf A.Saeed
 
Lesson 8- NodeMCU with Servo Motor
Elaf A.Saeed
 
Lesson 7- NodeMCU with DC Motor
Elaf A.Saeed
 
Lesson 6 - NodeMCU with PWM Pin
Elaf A.Saeed
 
lesson4 - NodeMCU control led
Elaf A.Saeed
 
lesson2 - Nodemcu course - NodeMCU dev Board
Elaf A.Saeed
 
lesson1 - Getting Started with ESP8266
Elaf A.Saeed
 
Embedded system course projects - Arduino Course
Elaf A.Saeed
 
Embedded system introduction - Arduino Course
Elaf A.Saeed
 
Pyton with rasperry pi
Elaf A.Saeed
 
Summary of MATLAB Functions-Part1
Elaf A.Saeed
 
MATLAB Basics-Part1
Elaf A.Saeed
 
Ad

Recently uploaded (20)

PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 

Python basics_ part1

  • 3. What is Python? Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: • web development (server-side), • software development, • mathematics, • system scripting.
  • 4. What can Python do? • Python can be used on a server to create web applications. • Python can be used alongside software to create workflows. • Python can connect to database systems. It can also read and modify files. • Python can be used to handle big data and perform complex mathematics. • Python can be used for rapid prototyping, or for production-ready software development.
  • 5. Why Python? • 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, an object-oriented way or a functional way.
  • 7. Python Install • Many PCs and Macs will have python already installed. • To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe): C:UsersYour Name>python –version • If you find that you do not have python installed on your computer, then you can download it for free from the following website: https://www.python.org/
  • 8. 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. • The way to run a python file is like this on the command line: C:UsersYour Name>python helloworld.py • Where "helloworld.py" is the name of your python file.
  • 9. Python Quickstart • helloworld.py, which can be done in any text editor: print("Hello, World!")  result: Hello, World!
  • 10. The Python Command Line • To test a short amount of code in python sometimes it is quickest and easiest not to write the code in a file. This is made possible because Python can be run as a command line itself. • Type the following on the Windows, Mac or Linux command line: C:UsersYour Name>python • Or, if the "python" command did not work, you can try "py": C:UsersYour Name>py
  • 11. The Python Command Line • Example 1: print("Hello, World!") Result: Hello, World!
  • 13. The Python Command Line • 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.
  • 14. The Python Command Line • Single Line Comments Comments starts with a #, and Python will ignore them: Example1: #This is a comment print("Hello, World!") Example2: print("Hello, World!") #This is a comment
  • 15. The Python Command Line • Multi Line Comments Example1: #This is a comment #written in #more than just one line print("Hello, World!")
  • 16. The Python Command Line • Multi Line Comments Example2: """ This is a comment written in more than just one line """ print("Hello, World!")
  • 18. Creating Variables • Variables are containers for storing data values. • Unlike other programming languages, 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)
  • 19. 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)
  • 20. Variable Names Example: #Legal variable names: myvar = "John" my_var = "John" _my_var = "John" myVar = "John" MYVAR = "John" myvar2 = "John" #Illegal variable names: 2myvar = "John" my-var = "John" my var = "John"
  • 21. Assign Value to Multiple Variables 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)
  • 22. Assign Value to Multiple Variables And you can assign the same value to multiple variables in one line: Example x = y = z = "Orange" print(x) print(y) print(z)
  • 23. Output Variables The Python print statement is often used to output variables. To combine both text and a variable, Python uses the + character: Example: x = "awesome" print("Python is " + x)
  • 24. Output Variables You can also use the + character to add a variable to another variable: Example: x = "Python is " y = "awesome" z = x + y print(z)
  • 25. Output Variables For numbers, the + character works as a mathematical operator: Example: x = 5 y = 10 print(x + y)
  • 26. Output Variables If you try to combine a string and a number, Python will give you an error: Example: x = 5 y = "John" print(x + y)
  • 27. Global Variables • Variables that are created outside of a function (as in all of the examples above) are known as global variables. • Global variables can be used by everyone, both inside of functions and outside. Example Create a variable outside of a function, and use it inside the function x = "awesome" def myfunc(): print("Python is " + x) myfunc()
  • 28. Global Variables 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) Result: Python is fantastic Python is awesome
  • 29. 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) Result: Python is fantastic
  • 30. The global Keyword Example To change the value of a global variable inside a function, refer to the variable by using the global keyword: x = "awesome" def myfunc(): global x x = "fantastic" myfunc() print("Python is " + x) Result: Python is fantastic
  • 32. Built-in Data Types In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories: Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool Binary Types: bytes, bytearray, memoryview
  • 33. Getting the Data Type You can get the data type of any object by using the type() function: Example Print the data type of the variable x: x = 5 print(type(x)) Result: <class 'int'>
  • 35. Python Numbers There are three numeric types in Python: • int • float • Complex Variables of numeric types are created when you assign a value to them: Example x = 1 # int y = 2.8 # float z = 1j # complex
  • 36. Python Numbers To verify the type of any object in Python, use the type() function: Example Result: <class 'int'> <class 'float'> <class 'complex'>
  • 37. Python Numbers Int Int, or integer, is a whole number, positive or negative, without decimals, of unlimited length. Example Result: <class 'int'> <class 'int'> <class 'int'>
  • 38. Python Numbers Float Float, or "floating point number" is a number, positive or negative, containing one or more decimals. Example Result: <class 'float'> <class 'float'> <class 'float'>
  • 39. Python Numbers Complex Complex numbers are written with a "j" as the imaginary part: Example Result: <class 'complex'> <class 'complex'> <class 'complex'>
  • 40. Python Numbers Type Conversion You can convert from one type to another with the int(), float(), and complex() methods: Example Result: 1.0 2 (1+0j) <class 'float'> <class 'int'> <class 'complex'>
  • 41. Python Numbers Note: You cannot convert complex numbers into another number type. Random Number Python does not have a random() function to make a random number, but Python has a built-in module called random that can be used to make random numbers: Example Result: 1.0 2 (1+0j) <class 'float'> <class 'int'> <class 'complex'>
  • 43. Eng. Elaf Ahmed Saeed linkden: www.linkedin.com/in/elaf-a-saeed-97bbb6150 facebook: https://www.facebook.com/profile.php?id=100004305557442 github: https://github.com/ElafAhmedSaeed youtube: https://youtube.com/channel/UCE_RiXkyqREUdLAiZcbBqSg slideshare: https://www.slideshare.net/ElafASaeed Slideplayer: https://slideplayer.com/slide/18029243/ Google Scholar: https://scholar.google.com/citations?user=VIpVZKkAAAAJ&hl=ar&gmla=AJsN- F7PIgAjWJ44Hzb18fwPqJaaUmG0XzbLdzx09