0% found this document useful (0 votes)
4 views4 pages

Basic Python Examples for Beginners

The document provides basic Python programming examples, including printing a message, using variables, conditional statements, loops, functions, and user input. Each example is accompanied by code snippets that illustrate the concepts. Topics covered include data types, decision-making, iteration, and simple arithmetic operations.
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)
4 views4 pages

Basic Python Examples for Beginners

The document provides basic Python programming examples, including printing a message, using variables, conditional statements, loops, functions, and user input. Each example is accompanied by code snippets that illustrate the concepts. Topics covered include data types, decision-making, iteration, and simple arithmetic operations.
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

Basic Python Examples with Explanations

1. Hello World

This is the most basic program in any language. It prints a simple message.

Code:

print("Hello, World!")

2. Variables and Data Types

Variables store data. Python supports types like string, integer, and boolean.

Code:

name = "Gowthami"

age = 21

is_student = True

print(name, age, is_student)

3. If-Else Condition

Conditions allow decision-making based on values.

Code:

marks = 85

if marks >= 90:

print("Grade: A")

elif marks >= 75:

print("Grade: B")
Basic Python Examples with Explanations

else:

print("Grade: C")

4. For Loop

Loops are used to repeat a block of code a number of times.

Code:

for i in range(1, 6):

print("Number:", i)

5. While Loop

Executes as long as the condition is True.

Code:

i=1

while i <= 5:

print("Count:", i)

i += 1

6. Function Example

Functions help reuse code. They are defined using `def`.

Code:

def greet(name):
Basic Python Examples with Explanations

print("Hello", name)

greet("Gowthami")

7. List and Loop

Lists store multiple values. We can loop through them.

Code:

fruits = ["apple", "banana", "mango"]

for fruit in fruits:

print(fruit)

8. Simple Calculator

Demonstrates arithmetic operations like add, subtract, multiply, divide.

Code:

a = 10

b=5

print("Add:", a + b)

print("Subtract:", a - b)

print("Multiply:", a * b)

print("Divide:", a / b)

9. Taking Input from User


Basic Python Examples with Explanations

Use `input()` to get user input from keyboard.

Code:

name = input("Enter your name: ")

print("Welcome", name)

10. Check Even or Odd

Checks if a number is even or odd using modulus operator `%`.

Code:

num = int(input("Enter a number: "))

if num % 2 == 0:

print("Even number")

else:

print("Odd number")

Common questions

Powered by AI

Data types in Python are crucial for understanding how variables can be used and manipulated. Python supports basic data types such as strings, integers, and booleans, each of which behaves differently under operations. For example, 'name = "Gowthami"', 'age = 21', and 'is_student = True' show string, integer, and boolean types. Operations applied to these types, like concatenation for strings or arithmetic for integers, depend on their data type .

Python's for loop can calculate the factorial of a number by iteratively multiplying sequence elements, reducing complex mathematical operations to repetitive tasks. For example, 'def factorial(n): result = 1 for i in range(1, n + 1): result *= i return result' computes factorial by iterating over the range from 1 to 'n' and multiplying the accumulating 'result' by each number in the loop. This effectively calculates n! .

Lists in Python are used to store multiple values, and loops can be utilized to process each element. For instance, 'fruits = ["apple", "banana", "mango"] for fruit in fruits: print(fruit)' iterates over the 'fruits' list, printing each fruit. This combination allows for efficient data processing and iteration over numerous items, whether for displaying values, performing computations, or modifying list contents .

Python’s if-else condition allows decision-making based on specified criteria by executing different code blocks for different conditions. In this example, the grade is determined based on the 'marks' variable. If 'marks' is greater than or equal to 90, it prints 'Grade: A'; if 'marks' is between 75 and 89 inclusive, it prints 'Grade: B'; otherwise, it prints 'Grade: C' .

Python handles loops using 'for' and 'while' constructs. A 'for' loop iterates over a sequence of numbers or items, executing a block of code for each element. For example, 'for i in range(1, 6): print("Number:", i)' prints numbers 1 through 5. A 'while' loop continues executing as long as its condition evaluates to True. For example, 'i = 1 while i <= 5: print("Count:", i) i += 1' increments 'i' and prints it until 'i' becomes greater than 5 .

A basic Python program can determine if a number is even or odd using the modulus operator. For instance, 'num = int(input("Enter a number: ")) if num % 2 == 0: print("Even number") else: print("Odd number")' reads an integer input, computes the remainder when divided by 2, and prints 'Even number' if the remainder is 0, or 'Odd number' otherwise .

Assignment and operation precedence dictate how expressions are evaluated in Python. Variables are assigned using '=', while operation precedence dictates the order of operations. For example, 'result = 10 + 5 * 2' yields 20 because multiplication precedes addition, emphasizing the need to understand precedence to prevent unintended computations. Parentheses can alter this order to 'result = (10 + 5) * 2', changing the outcome to 30 .

Arithmetic operations in Python, such as addition, subtraction, multiplication, and division, form the backbone of mathematical computations. A simple calculator program can be created using these operations to handle numeric calculations. For instance, 'a = 10 b = 5 print("Add:", a + b) print("Subtract:", a - b) print("Multiply:", a * b) print("Divide:", a / b)' performs basic arithmetic on the variables 'a' and 'b', showcasing how operations can be structured for simple calculations .

Python handles user input using the 'input()' function, which allows users to enter data that the program can process. For example, 'name = input("Enter your name: ") print("Welcome", name)' prompts the user to enter their name, then welcomes them by outputting 'Welcome' followed by the entered name .

Functions encapsulate reusable code blocks that perform a task, enhancing modularity and readability in Python programs. By defining a function using 'def', such as 'def greet(name): print("Hello", name)', we can call 'greet("Gowthami")' to print 'Hello Gowthami'. This promotes code reuse, as the same logic can be applied wherever needed by simply invoking the function .

You might also like