[Go to site: main page, start]

0% found this document useful (0 votes)
11 views3 pages

Basic Python Programs for Beginners

The document provides a series of basic Python programs aimed at beginners, covering fundamental concepts such as printing messages, using variables, taking user input, and implementing control structures like if-else statements and loops. It also introduces functions, lists, simple calculators, and dictionaries, illustrating each concept with code examples and explanations. This serves as a foundational guide for learning Python programming.

Uploaded by

Anas Tanha
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)
11 views3 pages

Basic Python Programs for Beginners

The document provides a series of basic Python programs aimed at beginners, covering fundamental concepts such as printing messages, using variables, taking user input, and implementing control structures like if-else statements and loops. It also introduces functions, lists, simple calculators, and dictionaries, illustrating each concept with code examples and explanations. This serves as a foundational guide for learning Python programming.

Uploaded by

Anas Tanha
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 Programs for Learning

1. Hello World

Code:
print("Hello, world!")

Explanation:
This is the most basic program in any language. It simply prints a message to the
screen.

2. Variables and Data Types

Code:
name = "Anas"
age = 25
height = 5.9
is_student = True

Explanation:
Variables store values. Python automatically assigns the data type (string, integer,
float, boolean).

3. Input from User

Code:
name = input("Enter your name: ")
print("Hello,", name)

Explanation:
input() takes input from the user. The value is stored in the variable name.

4. If-Else Statement

Code:
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")

Explanation:
if-else is used for decision making. This checks if a person is 18 or older.

5. For Loop

Code:
for i in range(1, 6):
print("Number:", i)
Basic Python Programs for Learning

Explanation:
for loop repeats a block of code. range(1, 6) means 1 to 5.

6. While Loop

Code:
count = 1
while count <= 5:
print("Count:", count)
count += 1

Explanation:
while loop runs as long as the condition is true.

7. Functions

Code:
def greet(name):
print("Hello", name)

greet("Anas")

Explanation:
Functions let you group code that performs a task. You can reuse it by calling the
function.

8. Lists and Loops

Code:
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)

Explanation:
A list stores multiple items. You can loop through it using for.

9. Simple Calculator

Code:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Sum is:", a + b)
print("Difference is:", a - b)
print("Product is:", a * b)
print("Division is:", a / b)

Explanation:
This performs basic arithmetic operations based on user input.
Basic Python Programs for Learning

10. Dictionary (Key-Value Pair)

Code:
student = {"name": "Anas", "age": 25, "grade": "A"}
print(student["name"])
print(student["age"])

Explanation:
A dictionary stores data in key: value pairs. You can access values using the keys.

Common questions

Powered by AI

The input() function in Python prompts the user for input and returns the entered data as a string, regardless of what is entered. To use it in other data types, you often need to explicitly convert it, such as using int() for integers or float() for floating-point numbers .

Functions in Python offer modularity and code reuse by allowing you to define a block of code that performs a task, which can be executed by calling the function name. This improves code organization and reduces redundancy. For example, defining a function def greet(name): encapsulates the task of greeting a person. You can reuse this by calling greet("Anas") whenever you need to greet someone .

A 'for' loop in Python is used to iterate over items of a sequence, such as a list. It allows automatic iterating through each item and executing a block of code for each item. For instance, in the loop: for fruit in fruits: print(fruit), Python iterates through the list ['apple', 'banana', 'mango'] and prints each fruit name .

Python automatically assigns data types to variables based on the values assigned. For instance, when you declare name = "Anas", Python assigns the string data type. Similarly, age = 25 assigns an integer type, height = 5.9 assigns a float type, and is_student = True assigns a boolean type .

A dictionary in Python is an unordered collection of data values, used to store data in key-value pairs. Each key is unique, and you can access its corresponding value by referencing the key. For example, student = {"name": "Anas", "age": 25, "grade": "A"} allows accessing values like student["name"] to get "Anas" .

Python lists can contain multiple data types, such as strings, integers, and booleans. This feature can be demonstrated using a loop that processes or prints each element. For example, a list example_list = ["apple", 42, True] can be iterated with for item in example_list: print(item), which outputs each element, showcasing Python's dynamic typing and flexibility in handling different data types within the same list .

Correct data type conversion is crucial because Python's dynamic typing system treats all input as strings by default, which are not suitable for arithmetic operations. To perform calculations, inputs must be converted to numerical types like int or float. Failing to convert can lead to runtime errors or logic errors since operations intended for numbers will not apply to strings, such as addition resulting in concatenation instead .

In Python, decision-making is implemented using 'if-else' statements. These statements evaluate a condition and execute certain code blocks based on whether the condition is true or false. For example, the if-else statement checks if a person is 18 or older with age = int(input("Enter your age: ")) and if age >= 18: executes one block, otherwise it executes the else block .

User input for arithmetic operations in Python is typically processed by using the input() function, followed by converting the input string to a numerical type using float() or int(). This allows the input values to be used in calculations. For example, numbers are retrieved using a = float(input("Enter first number: ")) and then arithmetic operations like addition, subtraction, multiplication, and division are performed using those inputs .

The primary difference is that a 'for' loop iterates over a sequence a specific number of times or through each item in an iterable, while a 'while' loop continues to execute as long as a specified condition is true. A 'for' loop is generally used when the number of iterations is known, like iterating through a list or using a range. In contrast, a 'while' loop is useful for indefinite iteration, where the condition depends on variables modified within the loop .

You might also like