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

4. DCIT22 Getting Started With Python 1

The document provides an introduction to Python, a powerful high-level programming language created by Guido van Rossum, with its first release in 1991. It covers Python's features, applications, indentation rules, identifiers, keywords, data types, operators, and input/output functions. The content is aimed at beginners learning the basics of Python programming.

Uploaded by

heliosinfinix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

4. DCIT22 Getting Started With Python 1

The document provides an introduction to Python, a powerful high-level programming language created by Guido van Rossum, with its first release in 1991. It covers Python's features, applications, indentation rules, identifiers, keywords, data types, operators, and input/output functions. The content is aimed at beginners learning the basics of Python programming.

Uploaded by

heliosinfinix
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

DCIT22-Computer Programming I

Lesson 3:
Getting Started
with Python

Prepared by:
Ms.Mariella R. Leyba, MIT
PYTHON

 Is a powerful high level programming


language.

 Python is a general-purpose language.


It has wide range of applications from
Web development (like: Django and
Bottle), scientific and mathematical
computing (Orange, SymPy, NumPy) to
desktop graphical user Interfaces
(Pygame, Panda3D).
GUIDO VAN ROSSUM

 The creator of Python

 Designing began in 1980s

 First release in February 1991

 “Monty Python’s Flying Circus”


Features of Python
1. A simple language which is easier to
learn.
2. Free and open-source
3. Portability
4. Extensible and Embeddable
5. A high level, interpreted language
6. Large standard libraries to solve
common task
7. Object-oriented
Applications of Python
 Web Applications
 Scientific and Numeric Computing
 Creating software Prototypes
 Good language to teach programming
PYTHON INDENTATION AND COMMENTS

 Indentation
● C, C++, and Java use curly braces {} to
define a block of code.
● Python uses indentation.

Example:
JAVA PYTHON

if (condition){ if condition: if condition:


System.out.print(“Hello”); print (‘Hello’) print (‘Hello’)
}
a=5 a=5
a = 5;

Incorrect indentation will result into IndentationError.


PYTHON INDENTATION AND COMMENTS

 Comments
● In Python, we use the hash (#) symbol
to start writing a comment. MULTI-LINE COMMENTS
- For multiple strings.
Example:
#This is a comment """This is also a
#print out Hello perfect example of
print('Hello') multi-line comments"""

Output:
Hello
PYTHON IDENTIFIER

 Identifier num = 1
identifier literal
● is the name given to entities like
class, functions, variables etc. in
Python.

Things to care about:


• Python is a case-sensitive language.
This means num1 and Num1 are not the same.
• Always name identifiers that make sense.
x=0 VS num=0
• Multiple words can be separated by an underscore.
example:
this_is_a_long_variable
• We can use camel-case style of writing.
example:
camelCaseExample
PYTHON IDENTIFIER
Rules for naming identifiers:
1. Identifiers can be a combination of letters in lowercase (a 3. Keywords cannot be used as identifiers.
to z) or uppercase (A to Z) or digits (0 to 9) or an
underscore (_).
4. We cannot use special symbols like !, @, #, $, %
Example of valid identifiers:
etc. in our identifier.
• myClass
• var_1
• print_this_to_screen >>> a@ = 0
File "<interactive input>", line 1
a@ = 0
^
2. An identifier cannot start with a digit.
1variable is invalid, but variable1 is perfectly fine.
5. Identifier can be of any length.
Example of a not valid identifiers:
• 1num

Example of a valid identifiers:


• num1
PYTHON KEYWORDS

 Keywords
● are the reserved words in Python.

Here are some examples of keywords in Python:


Keywords in Python programming language
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
PYTHON DATATYPES

● Every value in Python has a


data type.

There are various data types in Python:


1. Numbers
- Integers
- Floating Point
2. Strings
3. List
4. Set
5. Tuple
PYTHON DATA TYPES

1. Numbers

• Integers
>>> num = 1234
>>> print(num)
1234

• Floating Point
- A floating point number is accurate up to decimal places.

>>> num = 0.1234


>>> print(num)
0.1234
PYTHON DATA TYPES

2. Strings
- is a sequence of Unicode characters.

Single Quote
>>> sample = ‘This is a string.’
>>> print(sample)
‘This is a string.’

Double Quote
>>> sample = “This is a string.”
>>> print(sample)
‘This is a string.’
PYTHON DATA TYPES

3. List
- is an order sequence of items

>>> sample = [1, 2.2, ‘python’]


>>> print(sample)
[1, 2.2, ‘python’]

Index:
sample[0] = 1
sample[1] = 2.2
sample[2] = ‘python’

>>> sample = [1, 2.2, ‘python’]


>>> print(sample)
[1, 2.2, ‘python’]
>>> sample[1] = ‘hello’
>>> print(sample)
[1,’hello’, ‘python’]
PYTHON DATA TYPES

4. Set
- is an unordered collection of unique items.
- they eliminate duplicates.

>>> sample = {1, 2, 2, 3, 3, 3}


>>> print(sample)
{1, 2, 3}

5. Tuple
- is an ordered sequence of items same as list, difference is that it cannot be modified.

>>> sample = (5, ‘program’, 1.5)


>>> print(sample)
(5, ‘program’, 1.5)
Conversion between Data Types

● Integer to Float ● String to Float

● Float to Integer ● Integer to String

Do not do this:
Conversion between Data Types

● List to Set

● Set to Tuple

● String to List
PYTHON OPERATORS

 Operators
● are special symbols in Python that carry out arithmetic or logical computation.
● The value that the operator operates on is called the operand.

Here, + is the operator that performs addition. 2 and 3 are the operands and
5 is the output of the operation.
PYTHON OPERATORS

1. Arithmetic Operators
- are used to perform mathematical operations like addition, subtraction, multiplication etc.
PYTHON OPERATORS

2. Comparison Operators
- are used to compare values. It either returns True or False according to the condition.
PYTHON OPERATORS

3. Logical Operators
PYTHON OPERATORS

4. Assignment Operators
- Are used in Python to assign values to variables.
PYTHON OUTPUT

We use the print() function to output data to the standard output device.
PYTHON OUTPUT
● sep
- separator is used between the values. It defaults into a space.
print(1,2,3,4)
# Output: 1 2 3 4

print(1,2,3,4,sep='*')
# Output: 1*2*3*4

• Output Formatting
str.format() - this method is visible to any string object.
PYTHON INPUT
To allow flexibility we might want to take the input from the user.
In Python, we have the input() function to allow this.
Prompt - is the string we wish to display on the screen.

eval() - operation can be performed using this function. It can evaluate even expressions,
provided the input is a string.
Thank you and God bless! 

___

You might also like