DEV Community

KENNEDY NDUNGU WANJIRU
KENNEDY NDUNGU WANJIRU

Posted on

Python Basics

Variables. Data Types. Input/Output. Syntax.

Every programmer who you idolize began their journey right where you are, staring at a blinking cursor, searching for answers.

The good news is that all of them had to learn these four concepts:

Variables – the labeled boxes where a computer stores its information.

Data types – numbers, words, and booleans.

Input/output – the ways a program consumes and produces information.

Syntax – what makes computer languages different than English.

These concepts are the foundation of any successful coding journey. You won’t be able to build something meaningful without understanding them at a fundamental level.

Once you learn the basics, you’ll unlock the knowledge that can help you achieve extraordinary outcomes. Instagram and AI revolution are all built with computer programs.

This guide will give you an overview of the most important programming concepts in Python. It will explain what you need to know to become a better programmer, regardless of your goals. Some of these ideas are universal, and you’ll encounter them in any language. Moreover, Python is exceptionally beginner-friendly because it isolates these concepts and makes them easier to grasp.

Whether you just opened a Python book for the first time or haven’t touched the language in years, this guide will bring you up to speed. Let’s get started!

Assigning a variable

Assigning a variable in programming refers to the process of associating a value with a symbolic name (the variable name) so that the program can refer to and manipulate that value using the given name. It is essentially storing a piece of data in a memory location and giving it a label for convenient reference.
To assign a variable, we need three components:

  • Variable name: We can decide what we would like to name our variable but keep in mind it can be short and descriptive. We will learn about certain naming conventions we must follow in a bit.

  • Assignment operator: The variable name will be followed by the assignment operator. The assignment operator is an equal sign (=).

  • The value: The value we assign to a variable can be numerical or consist of characters, known as strings. In this part of the lesson, we will only be focusing on using numerical values.

plus

We assign values to variables for the following reasons:

  • To label and store information in memory.
  • This information can then be referenced later on and even be manipulated.
  • It makes it easier for a reader, and even ourselves, to understand our code.

examples
Iine
Many variables can be assigned to one value.
des

Rules

When naming variables, certain rules must be followed:

Rule Example
Must start with a letter or underscore myvarname or _myvarname
Not allowed to start with a number 2myvarname
Only allowed to contain alpha-numeric characters and underscores myvarname_2
Variable names are case-sensitive myvar $\neq$ Myvar

Case types

It is also important to be consistent when using a naming convention for variables.
We can use the following case types to ensure consistency:

Case type Description Example
Pascal case All the first letters in the variable name are capitalised, with no spaces and characters between the words MyVarName
Camel case The first word's letter is not capitalised, and it doesn't have any characters between the words myVarName
Snake case Words are separated by underscores and usually all lowercase my_var_name

t1

t2

t3

t4

t5

t6

Data types

Like other programming languages, variables in Python have values and types. Data types dictate what kind of data a variable can store and, in some cases, the amount of memory allocated to it. Allocating variable types allows us to make assumptions about the kind of operations that can be applied to a given variable.

Python makes use of five primitive variable types:

  • Integers
  • Floats
  • Complex numbers
  • Strings
  • Booleans

(a) Numbers
Three main data types exist to store numerical data:

  1. Integers (ints): These are positive or negative whole numbers with no fractional components. By using built-in functions in Python, such as type(), we can determine if a number is an integer or a float, for example, 5, -10.

  2. Floats: This is another numerical data type that is used to represent real numbers. A decimal point is used to divide the integer and fractional components of the value, for example, 3.14, -0.5.

  3. Complex numbers: A complex number is a distinct data type used to represent numbers in the form a + bj, where a and b are real numbers, and j is the imaginary unit (for example, the square root of -1). Complex numbers play a crucial role in various mathematical and engineering applications, and Python provides native support for them, for example, 2 + 3j, 1 - 1j.

Strings in Python are sequences of characters enclosed in quotes, either single (') or double ("). They are versatile and widely used for representing textual data.

Characteristics of strings include:

  • Sequence of characters: Strings can contain letters, numbers, symbols, and spaces.

Ip

  • Immutable: Once a string is defined, its content cannot be changed. Any operation that appears to modify a string actually creates a new string.

Im

(c) Booleans
Booleans are built-in data types capable of taking one of two possible values: True or False(interchangeable with the integers 1 and 0). Booleans control the flow of a program and are used to make comparisons, showing that despite the fancy name (nicknamed bool in Python) they play an integral role.

Boolean data types can be used with comparison and logical operators:

Comparison operators Description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Logical operators Description
and True if both are true(x and y). This operator is used to check whether both conditions are true.
or True if at least one is true (x or y). This operator is used to check if either of the conditions is true.
not True only if false (not x). This operator is used to check for inequality.

These boolean operators will either equate to True if the condition is met or False if the condition is not met.
Below we will be testing some conditions using logical operators.

ep

up

The print() function is used for output in various formats and the input() function enables interaction with users.

input() function is used to take user input and it returns the user input in form of a string.

Im

print() function allows us to display text, variables and expressions on the console.

de

Casting variables to different data types is a fundamental concept in programming that involves transforming a variable from one data type to another. In Python, this process is commonly referred to as "type casting" or simply "casting." The ability to cast variables enables programmers to adapt and manipulate data efficiently, ensuring that the correct operations, methods, and functions can be applied to specific types of data.
We can confirm data type using the following syntax
variable_type = type(variable)

on

tion

Casting data types is a more explicit or forceful change in type, normally actioned by the user.
new_variable = target_data_type(existing_variable)

I

I hope this article will be useful to you the reader and any python beginner.If you find this article useful share it widely.

Top comments (0)