Python Lessons 1: Basic Data Types and Variables: Explanations with Practical Examples
Share
Welcome to Python!
Hello friends! I am Ali, an experienced software developer, and I have prepared this guide for you to get started in the magical world of Python. Python is an easy-to-learn, powerful, and versatile programming language. It is used in web applications, data analysis, artificial intelligence, and many other areas. In this series of lessons, we will learn the basic concepts step by step, and you will be able to write your own Python programs in a short time.Basic Data Types
In Python, everything is considered a data type . Data types define what kind of data variables can hold. The most common data types are:
Numbers: Numbers include whole numbers (integers such as 10 25 -5) and decimals (float, for example 3.14 2.718).
Texts: Texts are called strings and are enclosed in double quotes. For example, "Hello world!" is a string.
Variables
Variables are names used to store data in our program. To create a variable, we use the equal sign (=).
Example: ```python name = "Ali" age = 32 ``` In the example above, we created variables name and age . The name variable stores the text "Ali" and the age variable stores the number 32.
Changing Variable Types
Since Python is a dynamic language, we can change the data type of variables at runtime. Example: ```python number = 10 # integer number = "On" # string ``` In this example, the number variable first stores an integer value and then stores a string value.Converting Data Types
Sometimes we need to convert one data type to another. Python provides special functions for this. Example: ```python number = "10" # string number = int(number) # convert string to integer print(number + 5) # outputs 15 ``` In this example, the int() function converts the number variable from string to integer.Practical Examples
Now let's reinforce what we have learned with practical examples:
1. Getting the Name and Age from the User and Printing them to the Screen ```python name = input("Your name: ") age = int(input("Your age: ")) print("Hello" name "!" age "years old.") ``` This program gets the name and age from the user and prints a greeting message to the screen. 2. I
Adding Two Numbers ```python number1 = int(input("Enter the first number: ")) number2 = int(input("Enter the second number: ")) total = number1 + number2 print("Total:" total) ``` This program takes two numbers from the user, adds them, and prints the total on the screen.