Unit I
Unit I
• Example
Variables
• Variable is a named placeholder to hold any type of data which the
program can use to assign and modify during the course of execution.
• In Python, there is no need to declare a variable explicitly by
specifying whether the variable is an integer or a float or any other
type.
• To define a new variable in Python, we simply assign a value to a
name.
Legal Variable Names
• Variable names can consist of any number of letters, underscores and
digits.
• Variable should not start with a number.
• Python Keywords are not allowed as variable names.
• Variable names are case-sensitive. For example, computer and
Computer are different variables.
Best Practices to be followed while naming a
variable
• Python variables use lowercase letters with words separated by
underscores as necessary to improve readability, like this whats_up,
how_are_you. Although this is not strictly enforced, it is considered a best
practice to adhere to this convention.
• Ensure variable names are descriptive and clear enough. This allows other
programmers to have an idea about what the variable is representing.
Reading Input
• variable_name = input([prompt])
Writing Output
• print(“Your text here”)
• print(variable name)
• There are two major string formats which are used inside the print()
function to display the contents onto the console as they are less
error prone and results in cleaner code.
• str.format();
• f=strings
str.format()
f-strings()
Assigning Values to Variables
• variable_name = expression
Operators
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
Arithmetic Operator
Assignment Operators
Comparison Operators
Logical Operators
Bitwise Operators
Precedence and Associativity
Data Types
• Numbers
• Boolean
• Strings
• None
Numbers
• Integers, floating point numbers and complex numbers fall under
Python numbers category.
• They are defined as int, float and complex class in Python. Integers
can be of any length; it is only limited by the memory available.
• A floating point number is accurate up to 15 decimal places.
• Integer and floating points are separated by decimal points.
• 1 is an integer, 1.0 is floating point number.
• Complex numbers are written in the form, x + yj, where x is the real
part and y is the imaginary part.
Boolean
• Boolean value, either True or False
• The Boolean values, True and False are treated as reserved words.
Strings
• A string consists of a sequence of one or more characters, which can
include letters, numbers, and other types of characters.
• A string can also contain spaces.
• You can use single quotes or double quotes to represent strings and it
is also called a string literal.
• Multiline strings can be denoted using triple quotes, ''' or """.
Indentation
• In Python, Programs get structured through indentation, Usually, we
expect indentation from any program code,
• but in Python it is a requirement and not a matter of style.
Comments
• Single Line Comment
• use the hash (#) symbol to start writing a comment.
'''This is
multiline comment
in Python using triple quotes'''
Type Conversions
• int()
• float()
Type Conversions
• str()
• chr()
Type Conversion
• complex()
Type Conversion
• ord()
Type Conversion
• hex()
• oct()
Control Flow Statements
Control Flow Statements
The if Decision Control Flow Statement
The if…else Decision Control Flow Statement
The if…elif…else Decision Control Statement
Nested if Statement
The while Loop
The for Loop
• Before executing the code in the source program, the Python interpreter
automatically defines few special variables.
• If the Python interpreter is running the source program as a stand-alone main
program, it sets the special built-in __name__ variable to have a string value
"__main__".
• After setting up these special variables, the Python interpreter reads the program
to execute the code found in it.
• All of the code that is at indentation level 0 gets executed. Block of statements in
the function definition is not executed unless the function is called.
Sample Programs
Sample Programs
Sample Programs
The return Statement and void Function
Example
Scope and Lifetime of Variables
• Python programs have two scopes: global and local.
• A variable is a global variable if its value is accessible and modifiable
throughout your program.
• A variable that is defined inside a function definition is a local variable.
Scope and Lifetime of Variables
Scope and Lifetime of Variables
Default Parameters
Keyword Arguments
• Until now you have seen that whenever you call a function with some
values as its arguments, these values get assigned to the parameters in the
function definition according to their position.
• In the calling function, you can explicitly specify the argument name along
with their value in the form kwarg = value.
• In the calling function, keyword arguments must follow positional
arguments.
• All the keyword arguments passed must match one of the parameters in
the function definition and their order is not important.
• No parameter in the function definition may receive a value more than
once.
Example
Example
Strings
Creating and Storing Strings
• Strings are another basic data type available in Python.
• They consist of one or more characters surrounded by matching
quotation marks.
Creating and Storing Strings
The str() Function
• The str() function returns a string which is considered an informal or
nicely printable representation of the given object.
• str(object)
Basic String Operation
• In Python, strings can also be concatenated using + sign and *
operator is used to create a repeated sequence of strings.
String Comparison
• You can use (>, <, <=, >=, ==,
!=) to compare two strings
resulting in either Boolean
True or False value.
• Python compares strings using
ASCII value of the characters.
Built-In Functions Used on Strings
Accessing Characters in String by Index
Number
string_name.join(sequence)
Example
Split Strings Using split() Method
• The split() method returns a list of string items by breaking up the
string using the delimiter string.
• The syntax of split() method is,
string_name.split([separator [, maxsplit]])
Strings Are Immutable
• As strings are immutable,
it cannot be modified.
The characters in a string
cannot bechanged once a
string value is assigned to
string variable.
String Traversing
• Since the string is a sequence of characters, each of these characters
can be traversed using the for loop.
String Methods
String Methods
String Methods
String Methods
String Methods
String Methods