Python
Beginner Python:
print("Hello, Python!")
print("My name is ………………..")
1- Variables
name = "Ali"
age = 15
print("My name is", name, "and I am", age, "years old.")
x = 10
y=3
print(x + y) # addition
print(x - y) # subtraction
print(x * y) # multiplication
print(x / y) # division
2- INPUT
name = input("What is your name? ")
print("Hello,", name, "!")
Python Conditions
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
These conditions can be used in several ways, most commonly in "if statements" and
loops.
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are still young!")
An "if statement" is written by using the if keyword.
EX:
a = 33
b = 200
if b > a:
print("b is greater than a")
Elif
The elif keyword is Python's way of saying "if the previous conditions were not true,
then try this condition".
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Else
The else keyword catches anything which isn't caught by the preceding conditions.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python Loop
Loop
Python has two primitive loop commands:
• while loops
• for loops
The while Loop
With the while loop we can execute a set of statements as long as a condition is true.
EX: Print X as long as X is less than 6:
X=1
while X < 6:
print(X)
X += 1
Exit the loop when X is 3
X=1
while X < 6:
print(X)
if X == 3:
break
X += 1
Continue to the next iteration if X is 3:
X=0
while X < 6:
X += 1
if X == 3:
continue
print(X)
Note that number 3 is missing in the result
The else Statement
With the else statement we can run a block of code once when the condition no longer
is true:
EX: Print a message once the condition is false:
X=1
while X < 6:
print(X)
X += 1
else:
print("X is no longer less than 6")
Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, adictionary,
a set, or a string).
This is less like the for keyword in other programming languages, and works more like
an iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list,
tuple.
Looping Through a String
EX : Even strings are inerrable objects, they contain a sequence of characters:
for x in "banana":
print(x)
The break Statement
With the break statement we can stop the loop before it has looped through all the
items:
EX: Exit the loop when x is "banana":
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
Python Lists
List
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data.
Lists are created using square brackets:
EX:
X = ["apple", "banana", "cherry"]
print(X)
• List Length
X = ["apple", "banana", "cherry"]
print(len(X))
• A list can contain different data types:
X = ["abc", 34, True, 40, "male"]
• Access Items
X = ["apple", "banana", "cherry"]
print(X[1])
X = ["apple", "banana", "cherry"]
print(X[-1])
X = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(X [2:5])
X = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(X[:4])
X = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(X[2:])
X = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(X[-4:-1])
X = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
X[1:3] = ["blackcurrant", "watermelon"]
print(X)
X = ["apple", "banana", "cherry"]
[Link](2, "watermelon")
print(X)
Game
import random
# Step 1: Computer picks a random number
secret_number = [Link](1, 20)
print(" Welcome to the Guessing Game!")
print("I have chosen a number between 1 and 20. Can you guess it?")
# Step 2: Player starts guessing
guess = None
while guess != secret_number:
guess = int(input("Enter your guess: "))
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(" Congratulations! You guessed it right.")
AI CODE
import random
print(" Hello! I am a simple AI chatbot.")
print("Type 'bye' to end the chat.")
# Some sample responses
responses = [
"That's interesting!",
"Can you tell me more?",
"Why do you think that?",
"I see... go on!",
"Hmm, I'm not sure about that.",
]
while True:
user_input = input("You: ")
if user_input.lower() == "bye":
print(" Goodbye! Nice talking to you.")
break
else:
# AI picks a random response
reply = [Link](responses)
print(" ", reply)