04.introduction To Python
04.introduction To Python
Ganpati Rathia
Assistant Professor
CEA Dept
GLA University
Python is an easy-to-learn, high-level programming language that
emphasizes readability and simplicity. It is widely used for various
applications, from web development to data science.
Understanding Python’s basic concepts is crucial before diving into
its advanced features.
1. Atoms
Identifiers: An identifier is a name given to variables, Keywords: Keywords are reserved words in Python
functions, classes, or other objects. It must start with that have special meanings. These cannot be used as
a letter (a-z, A-Z) or an underscore (_) and can contain identifiers.
letters, digits, and underscores. Identifiers are case- Examples include if, else, while, for, def, etc.
sensitive.
age = 25 # 'age' is an identifier List of Python keywords:
•import keyword
•print(keyword.kwlist)
3. Literals
name = "John"
message = 'Hello, World!'
multiline_string = '''This is
a multi-line
string.'''
4. Strings (Cont…)
• String Operations:
• Concatenation: +
• Repetition: *
• Slicing: string[start:end]
• Length: len(string)
greeting = "Hello"
name = "Alice"
full_message = greeting + " " + name
print(full_message) # Output: Hello Alice
5. Operators
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
5. Operators (Cont…)
5.4 Assignment Operators:
These are used to assign values to variables.
= : Assign
+= : Add and assign
-= : Subtract and assign
x = 10
*= : Multiply and assign
x += 5 # Equivalent to x = x + 5
/= : Divide and assign print(x) # Output: 15
//=: Floor divide and assign
%= : Modulo and assign
**=: Exponentiate and assign
5. Operators (Cont…)
5.5 Ternary Operator:
The ternary operator is a shorthand for the if-else statement. It follows the
syntax:
value_if_true if condition else value_if_false
age = 18
status = "Adult" if age >= 18 else "Minor"
print(status) # Output: Adult
5. Operators (Cont…)
5.6 Bitwise Operators:
Bitwise operators perform bit-level operations.
& : AND
| : OR
^ : XOR a = 5 # (in binary: 0101)
b = 3 # (in binary: 0011)
~ : NOT (complement) print(a & b) # Output: 1 (in binary: 0001)
<< : Left shift print(a | b) # Output: 7 (in binary: 0111)
>> : Right shift
5. Operators (Cont…)
5.7 Increment or Decrement Operators:
Python does not have ++ or -- operators like other languages.
Instead, you can use the addition and subtraction operators.
Example:
x=5
x += 1 # Increment
print(x) # Output: 6
x -= 1 # Decrement
print(x) # Output: 5
Sample Program Using Multiple Operator
Program to demonstrate various operators in Python # Assignment Operations
z=x
x = 10 print(f"z = x: {z}") # 10
y = 20
# Ternary Operator
# Arithmetic Operations result = "Greater" if x > y else "Smaller"
print(f"Addition: {x + y}") # 30 print(f"Result: {result}") # Smaller
print(f"Subtraction: {x - y}") # -10
print(f"Multiplication: {x * y}") # 200 # Bitwise Operations
print(f"Bitwise AND: {x & y}") # 0
# Relational Operations
print(f"x > y: {x > y}") # False # Increment/Decrement
print(f"x == y: {x == y}") # False x += 1
print(f"Incremented x: {x}") # 11
# Logical Operations
print(f"True and False: {True and False}") # False
Summary
• Atoms: Smallest units like identifiers, keywords, and literals.
• Identifiers: Names given to variables and functions.
• Keywords: Reserved words in Python (e.g., if, for).
• Literals: Fixed values (e.g., integers, strings).
• Strings: Sequence of characters enclosed in quotes.
• Operators: Perform operations on variables (e.g., arithmetic,
relational, logical).