0% found this document useful (0 votes)
1 views16 pages

04.introduction To Python

The document provides an introduction to Python, highlighting its readability and simplicity as a programming language. It covers fundamental concepts such as atoms, identifiers, keywords, literals, strings, and various types of operators including arithmetic, relational, logical, and bitwise. Understanding these basics is essential for progressing to more advanced Python features.

Uploaded by

ganpatikrathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views16 pages

04.introduction To Python

The document provides an introduction to Python, highlighting its readability and simplicity as a programming language. It covers fundamental concepts such as atoms, identifiers, keywords, literals, strings, and various types of operators including arithmetic, relational, logical, and bitwise. Understanding these basics is essential for progressing to more advanced Python features.

Uploaded by

ganpatikrathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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

In Python, atoms are the smallest units


of the language, which consist of:
• Identifiers
• Keywords
• Literals
2. Identifiers and Keywords

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

A literal is a notation used to represent a fixed


value in Python.
There are several types:
• Numeric literals: Integer, Float,
Complex
x = 100 # Integer literal
y = 20.5 # Float literal
• String literals: Enclosed in quotes
z = "Hello" # String literal
• Boolean literals: True and False
is_active = True # Boolean literal
• Special literal: None
4. Strings

• Strings in Python are sequences of


characters enclosed in either single
quotes (') or double quotes ("). Python
also supports multi-line strings using
triple quotes (''' or """).

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

5.1 Arithmetic Operators:


These operators are used to perform arithmetic
operations.
• + : Addition
• - : Subtraction
• * : Multiplication
• / : Division (float result)
• // : Floor division (integer result)
• % : Modulo (remainder)
Operators in Python are symbols used to • ** : Exponentiation
perform operations on variables and values.
a = 10
b=3
print(a + b) # Output: 13
print(a // b) # Output: 3
5. Operators (Cont…)
5.2 Relational Operators:
These operators compare two values and return True or False.
== : Equal to
!= : Not equal to x=5
> : Greater than y=3
< : Less than print(x > y) # Output: True
print(x == y) # Output: False
>= : Greater than or equal to
<= : Less than or equal to
5. Operators (Cont…)
• 5.3 Logical or Boolean Operators:
• These operators combine conditional statements.
and : Returns True if both conditions are true
or : Returns True if at least one condition is true
not : Reverses the result (returns True if the condition is false)

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).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy