Variables, Data Types, Operators and Functions

Objective : What I want you to learn?

Rationale : Why this is useful to you?

Learning Outcomes : What I want you to do after completing this topic?

Contents

Basic Syntax and Fundamentals

Python Identifiers

Python identifiers are names that are given to variables, functions, classes, modules, and other objects in Python. They are used to refer to these objects in Python code.

Here are the rules for writing Python identifiers:

Python is a case-sensitive programming language. 

Python Keywords

Python keywords are reserved words that have special meaning to the Python interpreter. They cannot be used as variable names, function names, or any other identifiers in your code. 

and,  as,  assert,  async,  await,  break,  continue, def,  del,  elif,  else,  except,  exec,  finally,  for,  from,  global,  if,  import,  in,  is,  lambda,  not,  or,  pass,  print,  raise,  return,   try,  while,  with,  yield 

Lines and Indentation

Lines and indentation are important concepts in Python. Python uses indentation to define blocks of code. A block of code is a group of statements that are executed together.

Multiline Statements

Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue 

total = item_one + \ 

item_two + \ 

item_three 

Statements contained within the [], {}, or () brackets do not need to use the line continuation character. 

days = ['Monday', 'Tuesday', 'Wednesday', 

'Thursday', 'Friday'] 

Quotations in Python

Quotations in Python are used to represent strings. Strings are sequences of characters, and they can be used to represent text, numbers, or other data. 

There are three types of quotations in Python:

Variable: Creating, Printing, and Deleting 

Variables are defined with the assignment operator, “=”. Python is dynamically typed, meaning that variables can be assigned without declaring their type, and that their type can change. Values can come from constants, from computation involving values of other variables, or from the output of a function. Python variables are containers for storing data values.

They are created the moment you assign a value to them, and you can use them to store any type of data, including numbers, strings, lists, dictionaries, and objects.

Example: 

my_number = 10 

my_string = "Hello, world!" 

my_list = [1, 2, 3

my_dictionary = {"name": "Alice", "age": 25} my_object = MyClass() 

To create a variable in Python, simply assign a value to it. For example, the following code creates a variable called name and assigns it the value "Kiran":

Once a variable has been created, you can access its value using its name. For example, the following code prints the value of the name variable to the console: 

To delete a variable in Python, you can use the del keyword. For example, the following code deletes the name variable: 

name = "Kiran" 

age = 33 

print(name) 

print(age) 

del name 

del age

Rules for Variable Names 

Tips for using variable names in Python:

Python Program for calculating Area of Rectangle by getting the input form the user

Local variables are defined inside a function and can only be accessed from within that function.

Local variables in Python are those which are initialized inside a function and belong only to that particular function. It cannot be accessed anywhere outside the function. 

Global variables are defined outside of any function and can be accessed from anywhere in your program.

These are those which are defined outside any function and which are accessible throughout the program, i.e., inside and outside of every function. 

Operators in Python

Python has a number of operators that can be used to perform operations on data types. 

Arithmetic operators:

Arithmetic operators in Python are used to perform mathematical operations on numbers. 

Comparison operators:

 Comparison operators in Python are used to compare the values of two operands. The operands can be any type of data, such as numbers, strings, lists, or objects. Comparison operators return a Boolean value, True or False, depending on whether the condition is met. 

Logical operators:

 Logical operators in Python are used to combine multiple Boolean expressions into a single Boolean expression. 


The three logical operators in Python are:

Bitwise operators:

Bitwise operators in Python are used to perform operations on the individual bits of an integer. 

Assignment operators:

 Assignment operators in Python are used to assign values to variables. 

In addition to the basic assignment operator, there are a number of other assignment operators in Python that can be used to combine arithmetic operations with assignment.

Data Types in Python

Python has a number of built-in data types, including:

Numbers -  Numbers can be integers, floats, or complex numbers. Integers are whole numbers, such as 1, 2, and 3. Floats are decimal numbers, such as 3.14 and 1.2e3. Complex numbers are numbers of the form a + bi, where a and b are real numbers and i is the imaginary unit.


Strings - Strings are sequences of characters. Strings can be represented in single quotes (') or double quotes (").


Lists - Lists are ordered collections of objects. Lists can be created using square brackets ([]). Lists can also contain other objects, such as strings, lists, and dictionaries. 


Tuples - Tuples are immutable ordered collections of objects. Tuples are created using parentheses (). Tuples are similar to lists, but they cannot be changed once they are created.


Sets - Sets are unordered collections of unique objects. Sets are created using curly braces ({ }). Sets can also contain other objects, such as strings, lists, and dictionaries. 


Dictionaries - Dictionaries are collections of key-value pairs. Dictionaries are created using curly braces ({ }) and colons (:)

# Create an integer

my_integer = 10


# Create a float

my_float = 3.14


# Create a complex number

my_complex_number = 1+2j


# Create a string

my_string = "Hello, world!"


# Create a list

my_list = [1, 2, 3, 4, 5]


# Create a tuple

my_tuple = (1, 2, 3, 4, 5)


# Create a set

my_set = {1, 2, 3, 4, 5}


# Create a dictionary

my_dictionary = {"name": "Bard", "age": 1

Functions in Python

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reuse. 

Python provides the following types of functions − 

To define a function in Python, you use the def keyword. 

The syntax is as follows: 

def function_name(parameter1, parameter2, ...): 

# Code block 

return value 

Example of a Simple Function

def add_numbers(a, b): 

return a + b 

result = add_numbers(1, 2

print(result) 

Calling a Function:

def factorial(n): 

return n * factorial(n - 1) 


factorial_result = factorial(5) 

Summary and Practice Assignment 

Summary

Practice Assignments