The second thing is the variables.
It represents the closet that keeps things inside it.
Executed Codeπ± :
a = "DevprintX"
print(a)
output :
DevprintX
Ofcourse you can't use any of python keywords as variable names or you will get syntax error like True = 'Yes' it will give you error but you can use yes = True.
Please open Telegram to view this post
VIEW IN TELEGRAM
π1
You can also use it in a different way.
Executed Codeπ± :
first_name = "DevprintX"
second_name = "Dev"
print(first_name + second_name)
output :
DevprintXDev
To be more organized, clean something small.
first_name = "DevprintX"
second_name = "Dev"
print(first_name + " " +second_name)
output :
DevprintX Dev
This allows us to add space between words.
Please open Telegram to view this post
VIEW IN TELEGRAM
Also.
first_name = "DevprintX"
print(f"your name is : {first_name}")
output :
your name is DevprintX
f means format Its name in Python is : f-string
Another exampleπ± :
age = 18
print(f"you are {age} years old")
output :
you are 18 years old
Please open Telegram to view this post
VIEW IN TELEGRAM
Today's lesson is : Type casting = the process of converting a variatile from one data type to another
str(), int(), float(), bool()
Executed Codeπ± :
name = "DevprintX"
age = 18
price = 10.99
is_student = True
print(type(name))
print(type(age))
print(type(price))
print(type(is_student))
output :
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
the explanation :
DevprintX is #string (DevprintX)
the nember 18 is #integer (18)
the nember 10.99 is #float (10.99)
True is #boolean (True)
Please open Telegram to view this post
VIEW IN TELEGRAM
note :
If we put any number or anything literally between quotation marks " "
The type will be (str) #string
Executed Codeπ± :
age = "18"
print(type(age))
output :
<class 'str'>
We can also change the type of anything.
Executed Codeπ± :
As we know that 18 is integer
number1 = str(18)
number2 = float(18)
number3 = bool(18)
print(type(number1))
print(type(number2))
print(type(number3))
output :
<class 'str'>
<class 'float'>
<class 'bool'>
Although it is an integer, the output came out like this, so we can conclude that we can change the type of anything.
Please open Telegram to view this post
VIEW IN TELEGRAM
Now we will work with input : Allows us to enter the information ourselves.
Executed Codeπ± :
age = int(input("Enter your age : "))
print(f"your age is {age}")The output will be the number you entered, For example, if we enter 18
your age is : 18
note : We put int so that the program accepts only correct numbers. For example, if we enter an incorrect name or number, an error will appear.
Please open Telegram to view this post
VIEW IN TELEGRAM
Example of an age calculation program by entering the current year and the year in which you were bornπ± .
year = int(input("Enter the current year : "))
birth_year = int(input("Enter your year of birth : "))
age = year - birth_year
print(f"your age is {age}")Code explanation : We will enter the current year and then the year of birth and it will give us our age. For example, if we enter 2025 as the current year and 2007 as the year of birth, the output will be :
your age is 18
Please open Telegram to view this post
VIEW IN TELEGRAM
Another exampleπ± :
Total Purchases Calculation.
item = input("Enter what you want to buy : ")
price = float(input("Enter the item price : "))
number = int(input("Enter how many you want to buy : "))
total = price * number
print(f"The price is : {total}")Code explanation : We enter the name of the item, its price, and how many we bought, and it will give us the total.
Please open Telegram to view this post
VIEW IN TELEGRAM
Now is the time to use mathπ¨ .
Codeπ± :
friends = 0
friends += 1
print(friends)
Although the friends variable is 0, because of this small code it becomes 1, and any number in the variable and any other variable adds one because of the small code, We can use any operation, not just (+) (* / -)
1
Please open Telegram to view this post
VIEW IN TELEGRAM
Now we will start with conditional sentences : if / else / elif
We will make the driving license code.π
Executed Codeπ± :
age = int(input("Enter your age : "))
if age >= 18:
print("You can get a driving license")
else:
print("You are still young")Code explanation : The command if age >= 18: It looks at the variable age and sees if the input is 18, meaning 18 years or older, it will tell us that we can get a driverβs license. Else means if it is not 18 or older, it tells us that we are still young.
We can add some modifications to overcome the errors.
Executed Codeπ± :
age = int(input("Enter your age : "))
if age >= 18:
print("You can get a driving license")
elif age <=0:
print("You cannot be 0 or younger")
else:
print("You are still young")The elif command is exactly like the if command, but we cannot use if more than once, which is why we use elif. It is also distinguished by the fact that we can use it an infinite number of times, unlike if.
Please open Telegram to view this post
VIEW IN TELEGRAM
Another simple code for you to understand
Another exampleπ± :
name = input("Enter your name : ")
if name == "":
print("Your name can't be nothing.")
else:
print(f"Hello {name}")Please open Telegram to view this post
VIEW IN TELEGRAM
Now with the mathematical operations
The idea : We will make a calculatorπ
process = input("Enter in operator (+ or - or * or /) : ")
num1 = float(input("Enter the 1nd number : "))
num2 = float(input("Enter the 2nd number : "))
try:
if process not in ["+", "-" , "*" , "/"]:
print("Check what you entered!Β‘.")
elif process == "+":
result = num1 + num2
print(result)
elif process == "-":
result = num1 - num2
print(result)
elif process == "*":
result = num1 * num2
print(result)
elif process == "/":
result = num1 / num2
print(result)
except ZeroDivisionError:
print("Cannot be divided by 0.")This is a simple calculator project in which we will explain everything.
Please open Telegram to view this post
VIEW IN TELEGRAM
Now we will continue with : (or / and / not)
Executed Codeπ± :
username = input("Enter the username : ").lower()
password = input("Enter the password : ").lower()
if username == "admin" and password == "devprintx":
print("You have successfully logged in")
elif not username or not password:
print("Please fill in all information")
else:
print("The information is wrong")Code explanation :
This code is a simple login page where we searched for all the fashions.
Please open Telegram to view this post
VIEW IN TELEGRAM
Now I will give you only one thing that will be very useful to you.
print(help(str))
while loop : This command is tasked with re-running any command if any of the conditions it sets are not met.
Executed Codeπ± :
name = input("Enter your name : ")
while name == "":
name = input("Enter your name : ")
age = int(input("Enter your age : "))
while age <= 0:
print("It seems you were not born yet")
age = int(input("Enter your age : "))
print(f"hello {name} you are {age} years old!")Code explanation :
In this code, if a person enters a blank space in the name, the process will repeat itself forever until he enters a real name or something. If he enters 0 or less in the age, the same thing happens.
Please open Telegram to view this post
VIEW IN TELEGRAM
Now we start with the Libraryπ±
The first library will be a telethon :
It is one of the most famous offices for creating sessions in accounts and bots and enjoys great fame.
import telethon #library hostility
Please open Telegram to view this post
VIEW IN TELEGRAM
The first code will be
Executed Codeπ± :
from telethon import TelegramClient, events
api_id = #your api id
api_hash = #your api hash
token = #your token bot
bot = TelegramClient("session",api_id, api_hash).start(bot_token=token)
This is the most important thing.
Please open Telegram to view this post
VIEW IN TELEGRAM