SlideShare a Scribd company logo
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python
(Lecture # 02)
by
Muhammad Haroon
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.
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("This is the first python program!")
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
Example
print("This is the first python program!") #This is a comment
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Comments does not have to be text to explain the code, it can also be used to prevent Python from
executing code:
Example
#print("Muhammad Haroon!")
print("Muhammad Haroon!")
Multi Line Comments
Python does not really have a syntax for multi line comments.
To add a multiline comment you could insert a # for each line:
Example
#This is a comment
#written in
#more than just one line
print("Muhammad Haroon!")
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Or, not quite as intended, you can use a multiline string.
Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string
(triple quotes) in your code, and place your comment inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Muhammad Haroon!")
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you
have made a multiline comment.
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 = 786
y = "Muhammad Haroon"
print(x)
print(y)
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Variables do not need to be declared with any particular type and can even change type after they have
been set.
Example
x = 786 # x is of type int
x = "Muhammad Haroon" # x is now of type str
print(x)
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
String variables can be declared either by using single or double quotes:
Example
x = "Muhammad Haroon"
# is the same as
x = 'Muhammad Haroon'
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, bikename, total_value).
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)
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:
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Example
Print the data type of the variable x:
x = 786
print(type(x))
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Example Data Type Result
x = "Muhammad Haroon" str 01.py
x = 786 int 02.py
x = 20.5 float 03.py
x = 1j complex 04.py
x = ["apple", "banana", "cherry"] list 05.py
x = ("apple", "banana", "cherry") tuple 06.py
x = range(6) range 07.py
x = {"name" : "Muhammad Haroon", "age" : 27} dict 08.py
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
x = {"apple", "banana", "cherry"} set 09.py
x = frozenset({"apple", "banana", "cherry"}) frozenset 10.py
x = True bool 11.py
x = b"Muhammad Haroon" bytes 12.py
x = bytearray(5) bytearray 13.py
x = memoryview(bytes(5)) memoryview 14.py
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor functions:
Example Data Type Result
x = str("Muhammad Haroon") str 15.py
x = int(786) int 16.py
x = float(10.5) float 17.py
x = complex(1j) complex 18.py
x = list(("apple", "banana", "cherry")) list 19.py
x = tuple(("apple", "banana", "cherry")) tuple 20.py
x = range(6) range 21.py
x = dict(name="Muhammad Haroon", age=27) dict 22.py
x = set(("apple", "banana", "cherry")) set 23.py
x = frozenset(("apple", "banana", "cherry")) frozenset 24.py
x = bool(5) bool 25.py
x = bytes(5) bytes 26.py
x = bytearray(5) bytearray 27.py
x = memoryview(bytes(5)) memoryview 28.py
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
End

More Related Content

What's hot (20)

PDF
Lecture20 xing
Tianlu Wang
 
PPTX
Conformer-Kernel with Query Term Independence @ TREC 2020 Deep Learning Track
Bhaskar Mitra
 
PDF
Final-Report
Ben Reichert
 
DOCX
Bc0038– data structure using c
hayerpa
 
PDF
Exposé Ontology
Joaquin Vanschoren
 
PPTX
Dual Embedding Space Model (DESM)
Bhaskar Mitra
 
PDF
Analysis of Similarity Measures between Short Text for the NTCIR-12 Short Tex...
KozoChikai
 
PDF
OpenML NeurIPS2018
Joaquin Vanschoren
 
PDF
OpenML 2019
Joaquin Vanschoren
 
PDF
Language Models for Information Retrieval
Nik Spirin
 
PDF
Learning how to learn
Joaquin Vanschoren
 
PPTX
Adversarial and reinforcement learning-based approaches to information retrieval
Bhaskar Mitra
 
PPTX
5 Lessons Learned from Designing Neural Models for Information Retrieval
Bhaskar Mitra
 
PPTX
Summary distributed representations_words_phrases
Yue Xiangnan
 
PPT
Computing with Directed Labeled Graphs
Marko Rodriguez
 
PDF
Bytewise Approximate Match: Theory, Algorithms and Applications
Liwei Ren任力偉
 
PPT
Probabilistic Models of Novel Document Rankings for Faceted Topic Retrieval
YI-JHEN LIN
 
PPTX
Tdm probabilistic models (part 2)
KU Leuven
 
Lecture20 xing
Tianlu Wang
 
Conformer-Kernel with Query Term Independence @ TREC 2020 Deep Learning Track
Bhaskar Mitra
 
Final-Report
Ben Reichert
 
Bc0038– data structure using c
hayerpa
 
Exposé Ontology
Joaquin Vanschoren
 
Dual Embedding Space Model (DESM)
Bhaskar Mitra
 
Analysis of Similarity Measures between Short Text for the NTCIR-12 Short Tex...
KozoChikai
 
OpenML NeurIPS2018
Joaquin Vanschoren
 
OpenML 2019
Joaquin Vanschoren
 
Language Models for Information Retrieval
Nik Spirin
 
Learning how to learn
Joaquin Vanschoren
 
Adversarial and reinforcement learning-based approaches to information retrieval
Bhaskar Mitra
 
5 Lessons Learned from Designing Neural Models for Information Retrieval
Bhaskar Mitra
 
Summary distributed representations_words_phrases
Yue Xiangnan
 
Computing with Directed Labeled Graphs
Marko Rodriguez
 
Bytewise Approximate Match: Theory, Algorithms and Applications
Liwei Ren任力偉
 
Probabilistic Models of Novel Document Rankings for Faceted Topic Retrieval
YI-JHEN LIN
 
Tdm probabilistic models (part 2)
KU Leuven
 

Similar to Lecture02 - Fundamental Programming with Python Language (20)

PDF
The python fundamental introduction part 1
DeoDuaNaoHet
 
PPTX
python_class.pptx
chandankumar943868
 
PPTX
IOT notes,................................
taetaebts431
 
PPTX
Introduction to python for the abs .pptx
Mark Musah Ibrahim
 
PPTX
funadamentals of python programming language (right from scratch)
MdFurquan7
 
PPTX
Python-Basics.pptx
TamalSengupta8
 
PDF
Python Programming
Saravanan T.M
 
PPTX
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
PPTX
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
PPTX
Chapter7-Introduction to Python.pptx
lemonchoos
 
PPTX
Python PPT.pptx
JosephMuez2
 
PPT
Python tutorialfeb152012
Shani729
 
PPT
Python - Module 1.ppt
jaba kumar
 
PPTX
BASICS OF PYTHON usefull for the student who would like to learn on their own
Nandini485510
 
PDF
Introduction to Python - Jouda M Qamar.pdf
EngjoudaQamar
 
PPTX
Python in 30 minutes!
Fariz Darari
 
PDF
Learn Python 3 for absolute beginners
KingsleyAmankwa
 
PPTX
introduction to python
Jincy Nelson
 
PPTX
Python programming
Ganesh Bhosale
 
PDF
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
The python fundamental introduction part 1
DeoDuaNaoHet
 
python_class.pptx
chandankumar943868
 
IOT notes,................................
taetaebts431
 
Introduction to python for the abs .pptx
Mark Musah Ibrahim
 
funadamentals of python programming language (right from scratch)
MdFurquan7
 
Python-Basics.pptx
TamalSengupta8
 
Python Programming
Saravanan T.M
 
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
Python (Data Analysis) cleaning and visualize
IruolagbePius
 
Chapter7-Introduction to Python.pptx
lemonchoos
 
Python PPT.pptx
JosephMuez2
 
Python tutorialfeb152012
Shani729
 
Python - Module 1.ppt
jaba kumar
 
BASICS OF PYTHON usefull for the student who would like to learn on their own
Nandini485510
 
Introduction to Python - Jouda M Qamar.pdf
EngjoudaQamar
 
Python in 30 minutes!
Fariz Darari
 
Learn Python 3 for absolute beginners
KingsleyAmankwa
 
introduction to python
Jincy Nelson
 
Python programming
Ganesh Bhosale
 
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
Ad

More from National College of Business Administration & Economics ( NCBA&E) (20)

PPTX
Lecturre 07 - Chapter 05 - Basic Communications Operations
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture # 02 - OOP with Python Language by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 06 - Chapter 4 - Communications in Networks
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 05 - Chapter 3 - Models of parallel computers and interconnections
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture01 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 04 (Part 01) - Measure of Location
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 04 chapter 2 - Parallel Programming Platforms
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 04 Chapter 1 - Introduction to Parallel Computing
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 03 Part 02 - All Examples of Chapter 02 by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 02 - Chapter 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 02 - Chapter 1 (Part 02): Grid/Cloud Computing Systems, Cluster Comp...
National College of Business Administration & Economics ( NCBA&E)
 
PPTX
Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...
National College of Business Administration & Economics ( NCBA&E)
 
PDF
WHO director-general's opening remarks at the media briefing on covid-19 - 23...
National College of Business Administration & Economics ( NCBA&E)
 
PDF
Course outline of parallel and distributed computing
National College of Business Administration & Economics ( NCBA&E)
 
Lecturre 07 - Chapter 05 - Basic Communications Operations
National College of Business Administration & Economics ( NCBA&E)
 
Lecture # 02 - OOP with Python Language by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 06 - Chapter 4 - Communications in Networks
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 05 - Chapter 3 - Models of parallel computers and interconnections
National College of Business Administration & Economics ( NCBA&E)
 
Lecture01 Part(B) - Installing Visual Studio Code On All Version Of Windows O...
National College of Business Administration & Economics ( NCBA&E)
 
Lecture01 - Fundamental Programming with Python Language
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 04 (Part 01) - Measure of Location
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 04 chapter 2 - Parallel Programming Platforms
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 04 Chapter 1 - Introduction to Parallel Computing
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 Part 02 - All Examples of Chapter 02 by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 - Chapter 02 - Part 02 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 - Synchronous and Asynchronous Communication - Concurrency - Fault...
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 03 - Chapter 02 - Part 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 02 - Chapter 01 - Probability & Statistics by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 02 - Chapter 1 (Part 02): Grid/Cloud Computing Systems, Cluster Comp...
National College of Business Administration & Economics ( NCBA&E)
 
Lecture 01 - Chapter 1 (Part 01): Some basic concept of Operating System (OS)...
National College of Business Administration & Economics ( NCBA&E)
 
WHO director-general's opening remarks at the media briefing on covid-19 - 23...
National College of Business Administration & Economics ( NCBA&E)
 
Course outline of parallel and distributed computing
National College of Business Administration & Economics ( NCBA&E)
 
Ad

Recently uploaded (20)

PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Horarios de distribución de agua en julio
pegazohn1978
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Dimensions of Societal Planning in Commonism
StefanMz
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 

Lecture02 - Fundamental Programming with Python Language

  • 1. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Fundamental Programming with Python (Lecture # 02) by Muhammad Haroon 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. Creating a Comment Comments starts with a #, and Python will ignore them: Example #This is a comment print("This is the first python program!")
  • 2. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Comments can be placed at the end of a line, and Python will ignore the rest of the line: Example print("This is the first python program!") #This is a comment
  • 3. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Comments does not have to be text to explain the code, it can also be used to prevent Python from executing code: Example #print("Muhammad Haroon!") print("Muhammad Haroon!") Multi Line Comments Python does not really have a syntax for multi line comments. To add a multiline comment you could insert a # for each line: Example #This is a comment #written in #more than just one line print("Muhammad Haroon!")
  • 4. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Or, not quite as intended, you can use a multiline string. Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it: Example """ This is a comment written in more than just one line """ print("Muhammad Haroon!")
  • 5. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment. 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 = 786 y = "Muhammad Haroon" print(x) print(y)
  • 6. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Variables do not need to be declared with any particular type and can even change type after they have been set. Example x = 786 # x is of type int x = "Muhammad Haroon" # x is now of type str print(x)
  • 7. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 String variables can be declared either by using single or double quotes: Example x = "Muhammad Haroon" # is the same as x = 'Muhammad Haroon' Variable Names A variable can have a short name (like x and y) or a more descriptive name (age, bikename, total_value). 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) 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:
  • 8. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 Example Print the data type of the variable x: x = 786 print(type(x)) Setting the Data Type In Python, the data type is set when you assign a value to a variable: Example Data Type Result x = "Muhammad Haroon" str 01.py x = 786 int 02.py x = 20.5 float 03.py x = 1j complex 04.py x = ["apple", "banana", "cherry"] list 05.py x = ("apple", "banana", "cherry") tuple 06.py x = range(6) range 07.py x = {"name" : "Muhammad Haroon", "age" : 27} dict 08.py
  • 9. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 x = {"apple", "banana", "cherry"} set 09.py x = frozenset({"apple", "banana", "cherry"}) frozenset 10.py x = True bool 11.py x = b"Muhammad Haroon" bytes 12.py x = bytearray(5) bytearray 13.py x = memoryview(bytes(5)) memoryview 14.py Setting the Specific Data Type If you want to specify the data type, you can use the following constructor functions: Example Data Type Result x = str("Muhammad Haroon") str 15.py x = int(786) int 16.py x = float(10.5) float 17.py x = complex(1j) complex 18.py x = list(("apple", "banana", "cherry")) list 19.py x = tuple(("apple", "banana", "cherry")) tuple 20.py x = range(6) range 21.py x = dict(name="Muhammad Haroon", age=27) dict 22.py x = set(("apple", "banana", "cherry")) set 23.py x = frozenset(("apple", "banana", "cherry")) frozenset 24.py x = bool(5) bool 25.py x = bytes(5) bytes 26.py x = bytearray(5) bytearray 27.py x = memoryview(bytes(5)) memoryview 28.py
  • 10. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 11. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 12. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 13. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 14. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 15. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 16. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 17. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 18. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 19. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 20. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 21. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 22. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
  • 23. Fundamental Programming with Python [email protected] CS/IT/SE/CSE Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761 End