Basic Python Examples for Beginners
Basic Python Examples for Beginners
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 .