Lecture 9,10,11&12 Notes
Lecture 9,10,11&12 Notes
Variable in Python
A Python variable is a reserved memory location to store values. In other words, a
variable in a python program gives data to the computer for processing.
Every value in Python has a data type. Different data types in Python are
Numbers,List, Tuple, Strings, Dictionary, etc. Variables can be declared by any
name or even alphabets like a, aa, abc, etc
Variable Naming Rules in Python
1. Variable name should start with letter (a-zA-Z) or underscore (_).
Valid: age , _age , Age
Invalid: 1age
2. In variable name, no special characters allowed other than underscore (_).
Valid: age_ , _age
Invalid: age_*
3. Variables are case sensitive.
age and Age are different, since variable names are case sensitive.
4. Variable name can have numbers but not at the beginning.
Example: Age1
5. Variable name should not be a Python keyword. Keywords are also called as
reserved words.
Example
pass, break, continue.. etc are reserved for special meaning in Python. So, we
should
not declare keyword as a variable name.
eg.
a=100
print (a)
Data types
Python Data types are the classification or categorization of data items. It
represents the kind of value that tells what operations can be performed on a
particular data. Since everything is an object in Python programming.
Python has 5 standard data types as
1.Numbers
2.List
3.Tuple
4. Strings
5. Dictionary
1.Numbers
The numeric data type in Python represents the data that has a numeric value. A
numeric value can be an integer, a floating number, or even a complex number.
These values are defined as Python int , Python float , and Python complex classes
in Python .
5. Dictionary
Dictionary is kind of hash table type. A dictionary key can be almost any Python
type, but are usually numbers or strings. Values, on the other hand, can be any
arbitrary Python object.
Python dictionary is like associative arrays or hashes found in Perl and consist
of key:value pairs. The pairs are separated by comma and put inside curly brackets
{}. To establish mapping between key and value, the semicolon':' symbol is put b
Dictionaries are enclosed by curly braces ({ }) and values can be assigned and
accessed using square braces ([]).
Open Compiler
dict = {}
This is one
This is two
Conditional statements in Python are used to execute certain blocks of code based
on specific conditions. These statements help control the flow of a program,
making it behave differently in different situations.
Output:
Hello Geek
Hello Geek
Hello Geek
In Else Block
Infinite While Loop in Python
If we want a block of code to execute infinite number of times then we can use the
while loop in Python to do so.
The code given below uses a ‘while’ loop with the condition (count == 0) and this
loop will only run as long as count is equal to 0. Since count is initially set to 0, the
loop will execute indefinitely because the condition is always true.
count = 0
while (count == 0):
print("Hello Geek")
For Loop in Python
For loops are used for sequential traversal. For example: traversing a list or string
or array etc. In Python, there is “for in” loop which is similar to foreach loop in
other languages. Let us learn how to use for loops in Python for sequential
traversals with examples.
For Loop Syntax:
for iterator_var in sequence:
statements(s)
n=4
for i in range(0, n):
print(i)
Output
0
1
2
3
Python Functions
Python Functions is a block of statements that return the specific task. The idea is
to put some commonly or repeatedly done tasks together and make a function so
that instead of writing the same code again and again for different inputs, we can
do the function calls to reuse code contained in it over and over again.