0% found this document useful (0 votes)
78 views

Python Chapter 1

Python Chapter 1 Diploma Sem 5 Gujarat technological University

Uploaded by

kitig31553
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Python Chapter 1

Python Chapter 1 Diploma Sem 5 Gujarat technological University

Uploaded by

kitig31553
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

OOPS & Python Programming

Course Code : 4351108

Chapter 1 :- Basics of Python


Unit Outcomes (UOs)
Topics and Sub-topics
1.1 Introduction to Python, Python Features, Python Applications
1.2 Installing Python
1.3 Basic Structure of Python Program, Keywords, Identifiers, Data types,
Variables, Operators, Type Casting
1.4 Input-Output functions: input, print
1.5 Introduction to Control Structures
1.6 Decision Making Structures: if, if-else statements, Nested if-else and if-elif-
else statements
1.7 Loops : for loop, while loop, Nested loops,
1.8 break, continue and pass statements
1.1 Introduction to Python, Python Features, Python Applications
Certainly! Here's a brief overview of Python:

Introduction to Python:
Python is a high-level, interpreted programming language known for its readability and
simplicity. Guido van Rossum created Python, and it was first released in 1991. Python's
design philosophy emphasizes code readability, and its syntax allows programmers to
express concepts in fewer lines of code than languages like C++ or Java.

Python Features:

1. **Easy to Learn and Read:**


Python's syntax is designed to be readable and straightforward, making it easy for
beginners to learn and understand.

2. **Versatile:**
Python is a general-purpose language used in various domains such as web
development, data science, machine learning, artificial intelligence, automation, and
more.

3. **Interpreted:**
Python is an interpreted language, which means that code can be executed line by
line, making it easy for debugging and testing.

4. **Dynamically Typed:**
Python is dynamically typed, meaning you don't need to specify the data type of a
variable explicitly. This makes development faster and code more flexible.
5. **Extensive Standard Library:**
Python comes with a vast collection of libraries and modules, providing pre-built
functions for a wide range of tasks.

6. **Community Support:**
Python has a large and active community of developers. This community support
includes forums, documentation, and numerous third-party libraries.

### Python Applications:


1. **Web Development:**
Frameworks like Django and Flask enable developers to build scalable and
maintainable web applications.
2. **Data Science and Machine Learning:**
Python is widely used in data analysis and machine learning. Libraries like NumPy,
Pandas, and TensorFlow are popular choices.
3. **Automation and Scripting:**
Python is excellent for automating repetitive tasks and writing scripts. It's often used
in system administration and network programming.
4. **Artificial Intelligence:**
Python is used in developing AI applications and is the language of choice for many
researchers and developers in the field.
5. **Game Development:**
Python has libraries like Pygame that make it suitable for developing simple games.

6. **Scientific Computing:**
Python is utilized in scientific computing applications due to its extensive libraries and
ease of integration with other languages.
7. **Desktop GUI Applications:**
Tools like Tkinter allow developers to create graphical user interfaces for desktop
applications.

1.2 Installing Python


Step 1: Download Python 3.9
To start, go to python.org/downloads and then click on the button to download the
latest version of Python:
Step 2: Run the .exe file
Next, run the .exe file that you just downloaded:
Step 3: Install Python 3.9
You can now start the installation of Python by clicking on Install Now:
Note that depending on your needs, you may also check the box to add Python to the
Path. Your installation should now begin:
After a short period of time, your setup would be completed:
Congrats, you just installed Python on Windows
Let's now see how to run a simple code in Python.
Run a Code in Python
You can run a code in Python via the Python IDLE,
A quick way to find your Python IDLE on Windows is by clicking on the Start menu. You
should then see the IDLE under "Recently added"
Once you click on the Python IDLE, you'll see the Shell screen:
Click on File and then select New File (alternatively, you may use the keyboard shortcut
of Ctrl+N):
1.3 Basic Structure of Python Program, Keywords, Identifiers, Data types,
Variables, Operators, Type Casting

Basic Structure of a Python Program:

# This is a simple Python program


# Comments start with a hash symbol (#) and are ignored by the interpreter

# Main code block


print("Hello, World!") # Print statement to display text

Keywords:

Keywords are reserved words in Python and cannot be used as identifiers (variable
names, function names, etc.). Examples include if, else, for, while, True, False, None,
etc.

if True:
print("This is an example of a keyword.")

Identifiers:

Identifiers are names given to entities like variables, functions, etc. They should follow
certain rules, such as starting with a letter or underscore, and can include letters, digits,
and underscores.

variable_name = 42
Data Types:

Python has several built-in data types, including int, float, str (string), bool (boolean),
list, tuple, dict (dictionary), and more.

integer_variable = 42
float_variable = 3.14
string_variable = "Hello, Python!"
Boolean variable = True

Variables:

Variables are used to store values. They are created by assigning a value to an identifier.

x = 10
y=5
sum_result = x + y
print("Sum:", sum_result)

Operators:

Python supports various operators, including arithmetic operators (+, -, *, /, %),


comparison operators (==, !=, <, >, <=, >=), logical operators (and, or, not), and more.

a = 10
b = 20

# Arithmetic operators
sum_result = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b

# Comparison operators
is_equal = (a == b)
is_not_equal = (a != b)
is_greater = (a > b)

# Logical operators
logical_and = (a > 0) and (b > 0)
logical_or = (a > 0) or (b > 0)
logical_not = not (a > 0)

# Print results
print("Sum:", sum_result)
print("Is equal?", is_equal)
print("Logical AND:", logical_and)
Type Casting:

Type casting involves converting one data type to another. Python supports functions
like int(), float(), str(), etc., for type casting.

# Type casting examples


float_number = 3.14
int_number = int(float_number) # Converts float to int
str_number = str(int_number) # Converts int to string

print("Original Float:", float_number)


print("After Int Casting:", int_number)
print("After Str Casting:", str_number)

1.4 Input-Output functions: input, print


Input Function (input()):
The input() function allows you to take user input in your Python programs. It
reads a line from the user as a string.
# Example of input function
user_name = input("Enter your name: ")
print("Hello, " + user_name + "!") # Concatenating strings to greet the user

Output Function (print()):

The print() function is used to display output in Python. It can print strings, variables,
and expressions.

# Example of print function


print("Hello, World!")
# Printing variables and expressions
x=5
y = 10
print("Sum:", x + y)

Combining Input and Output:

You can combine input and output functions to create interactive programs.

# Taking user input and performing a calculation


radius = float(input("Enter the radius of a circle: "))
area = 3.14 * radius ** 2
print("The area of the circle is:", area)
In this example, the user is prompted to enter the radius of a circle. The program then
calculates the area and prints the result.

1.5 Introduction to Control Structures


Control structures in Python are used to control the flow of a program, allowing you to
make decisions and repeat certain blocks of code. Here's a brief introduction to control
structures with examples:
1. Conditional Statements (if, else, elif):
Conditional statements are used to make decisions in your code.
# Example of if statement
x = 10
if x > 0:
print("Positive number")

# Example of if-else statement


y = -5
if y > 0:
print("Positive number")
else:
print("Non-positive number")

# Example of if-elif-else statement


grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
else:
print("C")

2. Looping Statements (for, while):


Looping statements are used to repeat a block of code.
# Example of for loop
for i in range(5):
print(i)

# Example of while loop


counter = 0
while counter < 5:
print(counter)
counter += 1
3. Control Statements (break, continue, pass):

Control statements alter the flow of the program.

# Example of break statement


for i in range(10):
if i == 5:
break
print(i)

# Example of continue statement


for i in range(5):
if i == 2:
continue
print(i)

# Example of pass statement


for i in range(3):
pass # Placeholder statement (used when syntactically needed, but no action is
desired)

4. Nested Control Structures:

Control structures can be nested inside one another.

# Example of nested if-else


x = 10
if x > 0:
if x % 2 == 0:
print("Positive even number")
else:
print("Positive odd number")
else:
print("Non-positive number")

1.6 Decision Making Structures: if, if-else statements, Nested if-else and if-elif-
else statements
. if Statement:
• The if statement is used for decision-making in Python. It executes a block of
code only if a specified condition is true.
x = 10
if x > 0:
print("Positive number")

2. if-else Statement:

• The if-else statement allows you to execute one block of code if the condition is true
and another if it's false.
y = -5
if y > 0:
print("Positive number")
else:
print("Non-positive number")
3. Nested if-else Statement:
• You can nest if-else statements inside each other for more complex decision-
making.
x = 10
if x > 0:
if x % 2 == 0:
print("Positive even number")
else:
print("Positive odd number")
else:
print("Non-positive number")
4. if-elif-else Statement:
• The if-elif-else statement allows you to check multiple conditions in sequence.
grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
else:
print("C")

1.7 Loops : for loop, while loop, Nested loops,


1. for Loop:
• The for loop is used to iterate over a sequence (such as a list, tuple, or string) or
other iterable objects.
# Example of for loop
for i in range(5):
print(i)
2. while Loop:
• The while loop is used to repeatedly execute a block of code as long as the
specified condition is true.
# Example of while loop
counter = 0
while counter < 5:
print(counter)
counter += 1

3. Nested Loops:
• You can nest loops inside each other to create more complex patterns or iterate
through multi-dimensional data structures.
# Example of nested loops
for i in range(3):
for j in range(2):
print(i, j)

1.8 break, continue and pass statements


1. break Statement:
• The break statement is used to exit a loop prematurely based on a certain
condition.
# Example of break statement
for i in range(5):
if i == 3:
break
print(i)
2. continue Statement:
• The continue statement is used to skip the rest of the code inside a loop
and move to the next iteration.
# Example of continue statement
for i in range(5):
if i == 2:
continue
print(i)

3. pass Statement:
• The pass statement is a null operation, and it is used when a statement is
syntactically required but you do not want to perform any action.
# Example of pass statement
for i in range(3):
pass # Placeholder statement, no action is taken

You might also like