Python Programing
Programming Python Basics

Python Tutorial – 1

Welcome to the Python Course! In this course, we will cover the basics of Python programming language. Python is a high-level programming language that is easy to learn and widely used in various industries such as web development, data science, machine learning, and artificial intelligence.

Lesson 01: Variables and Values

In Python, a variable is a name given to a memory location that stores a value. Variables can hold different types of values such as numbers, strings, and booleans. To create a variable in Python, we use the “=” sign to assign a value to a name. For example:

x = 10
name = "John"
is_student = True

In the above example, we created three variables “x”, “name”, and “is_student” and assigned them the values 10, “John”, and True respectively.

Assignments:

  • Create a variable “age” and assign it your age as an integer value.
  • Create a variable “city” and assign it the name of your city as a string value.
  • Create a variable “is_single” and assign it True or False based on your relationship status.
Lesson 02: Operators

Operators are special symbols in Python that perform various operations on variables and values. There are different types of operators in Python such as arithmetic operators, comparison operators, logical operators, and assignment operators.

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division. For example:

x = 10
y = 5
z = x + y
print(z)  # Output: 15

Comparison operators are used to compare two values and return True or False based on the result. For example:

x = 10
y = 5
print(x > y)  # Output: True

Logical operators are used to combine multiple conditions and return True or False based on the result. For example:

x = 10
y = 5
z = 20
print(x > y and z > x)  # Output: True

Assignment operators are used to assign a value to a variable. For example:

x = 10
x += 5  # Equivalent to x = x + 5
print(x)  # Output: 15

Assignments:

  • Create two variables “num1” and “num2” and assign them any integer values. Use the arithmetic operators to perform addition, subtraction, multiplication, and division operations between them.
  • Create two variables “str1” and “str2” and assign them any string values. Use the comparison operators to compare their lengths and print the result.
  • Create three variables “x”, “y”, and “z” and assign them any integer values. Use the logical operators to check if “x” is greater than “y” and “z” is greater than “x”.
Lesson 03: Data Types

In Python, there are different types of data such as numbers, strings, booleans, lists, tuples, and dictionaries. Each data type has its own properties and methods that can be used to perform various operations.

Numbers can be integers or floating-point numbers. For example:

x = 10    # Integer
y = 3.14  # Floating-point number

Strings are sequences of characters enclosed in single or double quotes. For example:

name = "John"
message = 'Hello, world!'

Boolean can have two values, True or False. For example:

is_student = True
is_adult = False

Lists are ordered collections of items enclosed in square brackets. For example:

numbers = [1, 2, 3, 4, 5]
names = ["John", "Mary", "Bob"]

Tuples are ordered collections of items enclosed in parentheses. For example:

numbers = (1, 2, 3)
names = ("John", "Mary", "Bob")

Dictionaries are unordered collections of key-value pairs enclosed in curly braces. For example:

person = {"name": "John", "age": 30, "is_student": True}

To check the data type of a variable, we use the type() function. For example:

x = 10
print(type(x))  # Output: <class 'int'>

We can also convert one data type to another using the following functions:

  • int() – converts a value to an integer
  • float() – converts a value to a floating-point number
  • str() – converts a value to a string
  • list() – converts a value to a list
  • tuple() – converts a value to a tuple
  • dict() – converts a value to a dictionary

For example:

x = 10.5
print(int(x))   # Output: 10
name = "John"
print(list(name))  # Output: ['J', 'o', 'h', 'n']

Assignments:

  • Create a variable “price” and assign it any floating-point value. Convert it to an integer value and print the result.
  • Create a list “fruits” containing the names of your favorite fruits. Convert it to a tuple and print the result.
  • Create a dictionary “person” containing your name, age, and city. Convert it to a list and print the result.

Congratulations! You have completed the Python Crash Course: Lesson 01-03. In the next lessons, we will cover more advanced topics such as functions, loops, and conditional statements.

Leave a Reply

Your email address will not be published. Required fields are marked *