SlideShare una empresa de Scribd logo
1 de 6
Descargar para leer sin conexión
1/6
October 28, 2022
Python Variables: What are they?
mrexamples.com/python/python-variables
This post aims to assist the reader with a detailed analysis of Python variables through
examples.
Python Variables
Python variables are containers for storing values. The type of variables can be declared
before using them. Once we assign a value to a variable, it becomes a variable. There is no
static typing in Python.
Python Variable Creation
There is no command for declaring a variable in Python when we speak about variables.
In the below example we have created a variable in Python.
Execute
Example
1
2
3
4
5
6
7
x = 8
y = "Elon Musk"
print(x)
print(y)
It is unnecessary to make variables of a specific type, and they can even switch types once
they have been assigned.
Execute
2/6
Example
1
2
3
4
5
6
mr = 8 # mr is of type int
ex = "Sally" # ex is now of type str
print(ex)
Casting:
Casting in Python variables allows you to specify the data type of a variable.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
a = str(3) # a will occur '3'
b = int(3) # b will occur 3
c = float(3) # c will occur 3.0
print(a)
print(b)
print(c)
3/6
Determine the type
Using the type() function, you can determine a variable’s data type.
Execute
Example
1
2
3
4
5
6
7
a = 8
b = "Elon"
print(type(a))
print(type(b))
Python Variable Types
In data types, data items are classified or categorized. In other words, it indicates what
operations can be performed on a particular data set. A Python variable is an instance
(object) of a class, since everything is an object in Python programming.
The following are Python’s standard data types:
1. Numeric.
2. Sequence Type.
3. Boolean.
4. Set.
5. Dictionary.
The next section of this tutorial will go into more detail about data types and casting.
What is better, a single quote or a double quote?
Variables that are declared as strings can either be declared as single quotes or double
quotes. Both are the preferred ways:
Example:
4/6
1
2
3
4
5
6
7
8
9
10
x = "Elon_Musk"
# Or
x = 'Elon_Musk'
print (x)
Case Sensitive:
As far as Python is concerned, variable names are case-sensitive.
This will create two variables:
Example:
1
2
3
4
5
6
7
8
9
10
a = 4
A = "Sally"
#A has a value Sally and a has digit 4 – both are different variables.
print( a )
print( A )
5/6
Python Output Variables
When outputting variables in Python, the print statement is often used.
Python uses the + character to combine text and variables:
The + character can also be used to add variables to another variable in Python:
The + character works as a mathematical operator for numbers:
Execute
Example
1
2
3
4
5
6
a = 3
b = 7
print(a + b)
Python will give you an error message if you try to combine a string and a number:
Execute
Example
1
2
3
4
5
6
a = 3
b = "Elon"
print(a + b)
Python variables should follow the following guidelines.
6/6
1. You must begin a variable name with a letter or an underscore.
2. There can be no number at the beginning of a variable name.
3. In variable names, alpha-numeric characters and underscores (A-z, 0-9, and _) are
allowed.
4. elon_musk, Elon_Musk, and ELON_MUSK are three different variables (Python
variables are case-sensitive).
5. A variable cannot be named using reserved words (keywords).

Más contenido relacionado

Similar a Python Variables

Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts Pavan Babu .G
 
TOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfTOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfEjazAlam23
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| FundamentalsMohd Sajjad
 
Python Tutorial Questions part-1
Python Tutorial Questions part-1Python Tutorial Questions part-1
Python Tutorial Questions part-1Srinimf-Slides
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfIda Lumintu
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in pythonsunilchute1
 
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdfJavier Crisostomo
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxCatherineVania1
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and commentsNilimesh Halder
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxfaithxdunce63732
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxNimrahafzal1
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning ParrotAI
 

Similar a Python Variables (20)

Phyton Learning extracts
Phyton Learning extracts Phyton Learning extracts
Phyton Learning extracts
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
TOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdfTOPIC-2-Expression Variable Assignment Statement.pdf
TOPIC-2-Expression Variable Assignment Statement.pdf
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Python Tutorial Questions part-1
Python Tutorial Questions part-1Python Tutorial Questions part-1
Python Tutorial Questions part-1
 
CS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdfCS-XII Python Fundamentals.pdf
CS-XII Python Fundamentals.pdf
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf
 
Python - variable types
Python - variable typesPython - variable types
Python - variable types
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Unit -1 CAP.pptx
Unit -1 CAP.pptxUnit -1 CAP.pptx
Unit -1 CAP.pptx
 
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docxCS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
 
Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning Introduction to Python for Data Science and Machine Learning
Introduction to Python for Data Science and Machine Learning
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
python
pythonpython
python
 

Más de Mr Examples

Python Strings Format
Python Strings FormatPython Strings Format
Python Strings FormatMr Examples
 
Python Online Compiler
Python Online CompilerPython Online Compiler
Python Online CompilerMr Examples
 
What Is Solidity
What Is SolidityWhat Is Solidity
What Is SolidityMr Examples
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings MethodsMr Examples
 
World Environment Day 2022
World Environment Day 2022World Environment Day 2022
World Environment Day 2022Mr Examples
 
World Bee Day 2022
World Bee Day 2022World Bee Day 2022
World Bee Day 2022Mr Examples
 

Más de Mr Examples (6)

Python Strings Format
Python Strings FormatPython Strings Format
Python Strings Format
 
Python Online Compiler
Python Online CompilerPython Online Compiler
Python Online Compiler
 
What Is Solidity
What Is SolidityWhat Is Solidity
What Is Solidity
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings Methods
 
World Environment Day 2022
World Environment Day 2022World Environment Day 2022
World Environment Day 2022
 
World Bee Day 2022
World Bee Day 2022World Bee Day 2022
World Bee Day 2022
 

Último

Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Último (20)

Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

Python Variables

  • 1. 1/6 October 28, 2022 Python Variables: What are they? mrexamples.com/python/python-variables This post aims to assist the reader with a detailed analysis of Python variables through examples. Python Variables Python variables are containers for storing values. The type of variables can be declared before using them. Once we assign a value to a variable, it becomes a variable. There is no static typing in Python. Python Variable Creation There is no command for declaring a variable in Python when we speak about variables. In the below example we have created a variable in Python. Execute Example 1 2 3 4 5 6 7 x = 8 y = "Elon Musk" print(x) print(y) It is unnecessary to make variables of a specific type, and they can even switch types once they have been assigned. Execute
  • 2. 2/6 Example 1 2 3 4 5 6 mr = 8 # mr is of type int ex = "Sally" # ex is now of type str print(ex) Casting: Casting in Python variables allows you to specify the data type of a variable. Example: 1 2 3 4 5 6 7 8 9 10 11 12 a = str(3) # a will occur '3' b = int(3) # b will occur 3 c = float(3) # c will occur 3.0 print(a) print(b) print(c)
  • 3. 3/6 Determine the type Using the type() function, you can determine a variable’s data type. Execute Example 1 2 3 4 5 6 7 a = 8 b = "Elon" print(type(a)) print(type(b)) Python Variable Types In data types, data items are classified or categorized. In other words, it indicates what operations can be performed on a particular data set. A Python variable is an instance (object) of a class, since everything is an object in Python programming. The following are Python’s standard data types: 1. Numeric. 2. Sequence Type. 3. Boolean. 4. Set. 5. Dictionary. The next section of this tutorial will go into more detail about data types and casting. What is better, a single quote or a double quote? Variables that are declared as strings can either be declared as single quotes or double quotes. Both are the preferred ways: Example:
  • 4. 4/6 1 2 3 4 5 6 7 8 9 10 x = "Elon_Musk" # Or x = 'Elon_Musk' print (x) Case Sensitive: As far as Python is concerned, variable names are case-sensitive. This will create two variables: Example: 1 2 3 4 5 6 7 8 9 10 a = 4 A = "Sally" #A has a value Sally and a has digit 4 – both are different variables. print( a ) print( A )
  • 5. 5/6 Python Output Variables When outputting variables in Python, the print statement is often used. Python uses the + character to combine text and variables: The + character can also be used to add variables to another variable in Python: The + character works as a mathematical operator for numbers: Execute Example 1 2 3 4 5 6 a = 3 b = 7 print(a + b) Python will give you an error message if you try to combine a string and a number: Execute Example 1 2 3 4 5 6 a = 3 b = "Elon" print(a + b) Python variables should follow the following guidelines.
  • 6. 6/6 1. You must begin a variable name with a letter or an underscore. 2. There can be no number at the beginning of a variable name. 3. In variable names, alpha-numeric characters and underscores (A-z, 0-9, and _) are allowed. 4. elon_musk, Elon_Musk, and ELON_MUSK are three different variables (Python variables are case-sensitive). 5. A variable cannot be named using reserved words (keywords).