Python Basics: Step-by-Step Guide
Python Basics: Step-by-Step Guide
To determine if a number is even or odd in Python, use the modulus operator (%) to check the remainder of the division of the number by 2. A number is even if num % 2 == 0, and odd otherwise. For instance, using the code: num = int(input('Enter a number: ')), if num % 2 == 0: print('Even') else: print('Odd'), you can input a number and the program will print 'Even' if it's even and 'Odd' if it's odd .
Lists in Python are ordered collections of items that can store items of different data types. They allow for dynamic updates, such as adding, deleting, or changing items. For instance, we declare a list as fruits = ['apple', 'banana', 'cherry']. We can access items using indices, e.g., fruits[0] returns 'apple'. Lists can be modified using functions such as append(), which adds an item to the end of the list; for example, fruits.append('mango') adds 'mango' to the list .
In Python, factorial can be calculated using a loop by multiplying a series of descending natural numbers with a product accumulator. Here is an example using a for loop: num = int(input('Enter a number: ')), fact = 1, for i in range(1, num+1): fact *= i, print('Factorial:', fact). The loop multiplies fact by each number starting from 1 up to the given number, and then prints 'Factorial:' followed by the computed factorial .
Conditional statements in Python are structured using if, elif, and else keywords. These statements execute code based on whether conditions evaluate to True or False. For example, the condition if age >= 18: print('You are an adult') checks if age is 18 or greater. If the condition is true, it prints 'You are an adult'; otherwise, it prints 'You are a minor' using the else clause .
A function in Python is a block of organized, reusable code used to perform a single, related action. Functions help break our program into smaller and modular chunks, which makes the program more manageable. A function is defined using the def keyword, followed by the function's name and parameters. For example, def greet(name): return 'Hello, ' + name defines a function that concatenates a greeting with a name. It is called by print(greet('Vimalarani')), resulting in 'Hello, Vimalarani' .
Python supports basic arithmetic operators such as addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and exponentiation (**). For example, given x = 10 and y = 3, x + y results in 13, x - y results in 7, x * y results in 30, x / y results in 3.33, x % y results in 1, and x ** y results in 1000 .
User input in Python is handled using the input() function, which reads a line from input, converts it into a string, and returns it. For instance, to get a user's name, you would write: name = input('Enter your name: '). This captures the input as a string and can be printed using print('Hello,', name).
Comments are used to explain code and make it more understandable for humans. They are implemented using the hash character (#). Any text following the # on that line is ignored by the Python interpreter .
Dictionaries in Python store data in key-value pairs and offer efficient data retrieval through keys. A dictionary can be created using curly braces with keys and values: student = {'name': 'Anu', 'age': 21, 'course': 'CSE'}. We access values by their keys, e.g., student['name'] returns 'Anu'. Typical operations include updating values, adding new key-value pairs, and accessing or modifying values through methods like get(), keys(), and pop(). For instance, student['age'] = 22 updates the age value .
For loops in Python iterate over a sequence (such as a list or range) and execute block code for each item in the sequence. For example, for i in range(5): print(i) prints numbers 0 through 4. While loops execute as long as a condition remains true. For example, count = 1 while count <= 5: print(count) count += 1 prints numbers 1 to 5 until the count exceeds 5 .