3. WHY PYTHON
It is a high-level, general-purpose
programming language
released in 1991
by Guido van Rossum.
Python is an interpreted language, meaning
that it does not need to be compiled before
it can be run.
This makes it ideal for scripting, prototyping,
and rapid development, as changes to the
code can be quickly tested without the need
for a lengthy compile step.
4. PYTHON KEYWORDS
Keywords are predefined, reserved words used in Python programming that
have special meanings to the compiler.
We cannot use a keyword as a variable name, function name, or any other
identifier. They are used to define the syntax and structure of the Python
language.
All the keywords except True, False and None are in lowercase and they must
be written as they are.
6. PYTHON IDENTIFIERS
Identifiers are the name given to variables, classes, methods, etc.
Here, language is a variable (an identifier) which holds the value
'Python'. We cannot use keywords as variable names as they are
reserved names that are built-in to Python.
The above code is wrong because we have used continue as a
variable name.
7. PYTHON IDENTIFIERS
Identifiers cannot be a keyword.
Identifiers are case-sensitive.
It can have a sequence of letters and digits. However,
it must begin with a letter or _. The first letter of an
identifier cannot be a digit.
It's a convention to start an identifier with a letter
rather _.
Whitespaces are not allowed.
We cannot use special symbols like !, @, #, $, and so
on.
Rules for Naming an Identifier
9. THINGS TO REMEMBER
Python is a case-sensitive language. This means, Variable and
variable are not the same.
Always give the identifiers a name that makes sense. While c
= 10 is a valid name, writing count = 10 would make more
sense, and it would be easier to figure out what it represents
when you look at your code after a long gap.
Multiple words can be separated using an underscore, like
this_is_a_long_variable.
12. PYTHON COMMENTS
In computer programming, comments are hints that we
use to make our code more understandable. Comments
are completely ignored by the interpreter.
#this is a comment
13. TYPES OF COMMENTS IN PYTHON
single-line comment
multi-line comment
In Python, there are two types of comments:
14. SINGLE LINE COMMENT
A single-line comment starts and ends in the same
line. We use the # symbol to write a single-line
comment.
15. MULTI-LINE COMMENT IN PYTHON
Python doesn't offer a separate way to write multiline comments.
However, there are other ways to get around this issue.
We can use # at the beginning of each line of comment on multiple lines.
Another way of doing this is to use triple quotes, either ''' or """
16. PYTHON VARIABLES, CONSTANTS AND LITERALS
PYTHON VARIABLES
In programming, a variable is a container (storage area) to hold data.
In programming, a variable is a container (storage area) to hold data.
As we can see from the above example, we use the assignment operator = to assign
a value to a variable.
17. CHANGING THE VALUE OF A VARIABLE IN PYTHON
In this part of the code, you're creating a variable site_name
and assigning it the value 'programiz.pro'. Then, you're using
the print function to display the value of the site_name
variable.
Here, you're reassigning a new value 'apple.com' to the
site_name variable. This line of code replaces the previous
value 'programiz.pro'. After the reassignment, when you print
the site_name variable again, you will see the new value:
The variable site_name is initially assigned the value 'programiz.pro'.
You print the value of site_name, which outputs 'programiz.pro'.
You reassign the variable site_name with the value 'apple.com'.
You print the value of site_name again, which now outputs 'apple.com'.
18. ASSIGNING MULTIPLE VALUES TO MULTIPLE VARIABLES
a is assigned the value 5
b is assigned the value 3.2
c is assigned the value 'Hello'
In this line of code, you're declaring three variables a,
b, and c, and you're assigning values to them using a
technique called unpacking.
The values being assigned are 5, 3.2, and 'Hello',
respectively. Python automatically assigns each
value to its corresponding variable based on their
positions.
After this line of code, the variables have the
following values:
Then, you use the print function to output the values
of these variables
19. ASSIGNING MULTIPLE VARIABLES AT ONCE
In this line of code, you are assigning the value 'programiz.com' to both the variables
site1 and site2. This is achieved using multiple assignment, where the same value is
assigned to multiple variables in a single line.
After this line of code, both site1 and site2 contain the value 'programiz.com'.
Then, you use the print function to output the values of both variables:
21. PYTHON LITERAL
Literals are representations of fixed values in a program. They can
be numbers, characters, or strings, etc. For example, 'Hello, World!',
12, 23.0, 'C', etc.
Literals are often used to assign values to variables or constants.
PYTHON NUMERIC LITERALS
Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3
different numerical types: Integer, Float, and Complex.
PYTHON BOOLEAN LITERALS
There are two boolean literals: True and False.
23. STRING AND CHARACTER LITERALS IN
PYTHON
Here, S is a character literal assigned to some_character.
Similarly, String literals are sequences of Characters enclosed in quotation marks.
Here, 'Python is fun' is a string literal assigned to some_string.
26. LIST LITERAL
A tuple is a collection of different data-type.
It is enclosed by the parentheses ‘()‘ and each element
is separated by the comma(,).
It is immutable.
27. LIST LITERAL
fruits ---- variable
append ---- adding an element
remove ---- removing an
element
variable[index] ----- accessing
the elements
variable[index] = element ----
modifying the element
28. PRACTICE EXERCISES
Task 1: Create a list of your favorite colors using a list
literal. ( 4 colors. Use favorite colors as the variable)
Task 2: Add a new color to the list
Task 3. Modify the element in index 3
Task 4: Print the second color in the list
Task 5: Iterate through the list and print each favorite
color
Task 6. Check if "any color you want" is in the list of
favorite colors
LIST LITERAL
29. TUPLE LITERAL
List literals are written within square brackets [ ].
Lists work similarly to strings -- use the len() function
and square brackets [ ] to access data, with the first
element at index 0.
30. TUPLE LITERAL
person ---- variable
variable[index] ----- accessing
the elements
tuple has no remove attribute
31. TUPLE LITERAL
book_info ---- variable
variable[index] ----- accessing the elements
tuple has no remove attribute
33. TUPLE LITERAL
book_info ---- variable
variable[index] ----- accessing the
elements
tuple has no remove attribute
add_book_info ---- new variable
34. PRACTICE EXERCISES
Task 1: Create a tuple containing the names of four
countries
Task 2: Task 2: Print the second country in the tuple
Task 3. Check if 'Germany' is in the tuple of countries
Task 4: Iterate through the tuple and print each country
Task 5: Attempt to change an element in the tuple (this
should result in an error)
TUPLE LITERAL
35. DICTIONARY LITERAL
The dictionary stores the data in the key-value pair. It
is enclosed by curly braces ‘{}‘ and each pair is
separated by the commas(,).
We can store different types of data in a dictionary.
'Dictionaries are mutable.
37. PRACTICE EXERCISES
Task 1: Creating a dictionary using a dictionary
literal car
May Include the brand, model, year and color
Task 2: Accessing values in the dictionary
DICTIONARY LITERAL
38. SET LITERAL
Set is the collection of the unordered data set. It is
enclosed by the {} and each element is separated by
the comma(,).
39. SET LITERAL
vowels & fruits ---- variable
Sets in Python are implemented as hash tables, which are designed
for fast membership tests and elimination of duplicates.
40. SET LITERAL
fruits ---- variable
add ---- add another
element
remove/discard ----
removing an element
Boolean ---- True/False
41. PRACTICE EXERCISES
Task 1: Create a set of programming languages (4)
Task 2: Add a new programming language to the set
Task 3: Remove 'a programming language' from the set
Task 4: Check if 'that programming language' is in the
set
Task 5: Print the updated set of programming
languages
SET LITERAL
43. DATA TYPES
In computer programming, data types specify the
type of data that can be stored inside a variable.
Here, 24 (an integer) is assigned to the num variable.
So the data type of num is of the int class.
46. PRACTICE EXERCISES
x = {"name" : "John", "age" : 36}
code = "Hello World"
places = ["Pototan", "Aklan", "Roxas"]
type = bytearray(6)
z = b"Data Structures"
1.
2.
3.
4.
5.
FIND THE DATA TYPE
47. PYTHON NUMERIC DATA TYPE
In Python, numeric data type is used to hold numeric values.
Integers, floating-point numbers and complex numbers fall under Python numbers
category. They are defined as int, float and complex classes in Python.
int - holds signed integers of non-limited length.
float - holds floating decimal points and it's accurate up to 15 decimal places.
complex - holds complex numbers.
We can use the type() function to know which class a variable or a value belongs to.