Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Built-in functions

In Python, we have a lot of built-in functions. Built-in functions are available to you globally, which means you don’t need to import or configure to use built-in functions. Some of the most commonly used Python built-in functions are as follows: Print (), len(), type(), int(), float(), STR (), input(), list(), dict(), min(), Max (), sum(), sorted(), open(), file(), help(), and dir(). In the table below, you’ll see an exhaustive list of Python’s built-in functions taken from the Python documentation.

Let’s open the Python shell and start using some of the most common built-in functions.

Let’s practice more by using different built-in functions

As you can see from the terminal above, Python has reserved words. We do not use reserved words to declare variables or functions. We’ll cover variables in the next section.

I’m sure you’re now familiar with the built-in functions. Let’s do the built-in functions one more time, and we’ll continue in the next section.

2. The variable

Variables store data in computer memory. Mnemonic variables are recommended in many programming languages. Mnemonic variables are variable names that are easy to remember and associate. Variables are memory addresses where data is stored. Do not name variables starting with digits, special characters, or hyphens. Variables can have a short name (e.g. X, Y, z), but more descriptive names (first name, last name, age, country/region) are strongly recommended.

Python variable name rules

  • Variable names must begin with a letter or underscore character
  • Variable names cannot start with a number
  • Variable names can contain only alphanumeric characters and underscores (Az, 0-9, and _)
  • Variable names are case sensitive (firstName, firstName, firstName, and firstName are different variables)

Let’s set a valid variable name

First Name Age Country City First Name Capital _ City _if # If we want to use the reserved word as the variable Year _2021 2021 current _YEAR_2021 Birth year Number 1 Quantity 2Copy the code

Invalid variable name

First $name number 1, 1Copy the code

We will use the standard Python variable naming style used by many Python developers. Python developers use the snake_case variable naming convention. For variables that contain more than one word (for example, first_name, last_name, engine_ROTation_speed), we use an underscore character after each word. The following example is a standard example of variable naming, requiring an underscore when the variable name is more than one word.

When we assign a data type to a variable, it is called a variable declaration. For example, in the example below, my name is assigned to the variable first_name. The equal sign is the assignment operator. Assignment means storing data in variables. The equal sign in Python is different from the equal sign in mathematics.

Example:

# in Python variable FIRST_NAME = 'Asabeneh' Surname = 'Yetayeh' Country = 'Finland City =' Helsinki 'Age = 250 IS_married = True skill = ['HTML', 'CSS', 'JS', 'camp ',' Python'] person_info = {' firstName ': 'Asabeneh',' lastName ': 'Yetayeh',' Country ':' Finland ', 'City' : 'Helsinki'}Copy the code

Let’s use the print() and len() built-in functions. The print function takes an unlimited number of arguments. Parameters are values that we can pass or put inside function parentheses, as shown in the following example.

Example:

print ( 'Hello, World! ') # text Hello, World! Print ('Hello',',',' World','! Print (len ('Hello, World! ')) # It just needs an argumentCopy the code

Let’s print and find out the length of the variable declared at the top:

Example:

Print (' Name length: ', first_name) print (' Name length: ', len (first_name)) print (' Name length: ', last_name) Print (' Name length: ', len (last_name)) print (' Country: ', country) print (' City: ', city) Print (' Age: ', age) print (' Married: ', is_married) print (' Skill: ', skill) Print (' Personal Info: ', person_info)Copy the code

2.1 Declare multiple variables in a single line

It is also possible to declare multiple variables in a single line:

Example:

First_name, last_name, country, age, is_married = 'Asabeneh', 'Yetayeh', 'Helsink', 250, True Country, age, is_married) Print (' First name: ', FIRST_NAME) print (' last name: ', last name) print (' Country: ', country) Print (' Age: ', age) print (' Married: ', is_ Married)Copy the code

Use the input() built-in function to get user input. Let’s assign the data from the user to the first_name and age variables. Example:

First_name = input (' what's your name? ') age = input (' How old are you? ') Print (name) print (age)Copy the code

2.2 Data Types

There are many data types in Python. To identify data types, we use the type built-in function. I want you to focus on getting a good understanding of the different data types. When it comes to programming, it’s all about data types. I introduced data types at the beginning, and it comes again, because every topic has to do with data types. We’ll cover data types in more detail in their respective sections.

2.3 Check data types and conversions

  • Checking data Types: To check the data types of some data/variables, we use the type example:
First_name = 'Asabeneh' # STR last_name = 'Yetayeh' # STR country = 'Finland' # STR City = 'Helsinki' # STR age = 250 # int, this is not my true age, Print (type ('Asabeneh')) # STR print (type (first_name)) # STR print (type (10)) # int print (type (3.14)) # float print (type(1 + 1j)) # complex print (type(True)) # bool print (type([1, 2, 3, 4 ])) # list print ( type ({ 'name' : 'Asabeneh' , 'age' : 250 , 'is_married' : 250})) # dict print (type ((1, 2)) # dict print (type (zip ([1, 2],[3, 4])) # setCopy the code
  • Conversion: To convert one data type to another. We use int(), float(), STR (), list, set When we do arithmetic, string numbers should be converted to int or float first, otherwise an error will be returned. If we concatenate a number with a string, the number should be converted to a string first. We will discuss connections in the string section.

Example:

# int to float num_int = 10 print ( 'num_int' , num_int ) # 10 num_float = float ( num_int ) print ( 'num_float:' , # 9 # int to STR num_int = 10 print (num_int) # 10 num_str = STR (num_int) print (num_str) # '10' # STR = '10.6' print ('num_int', int ( num_str )) # 10 print ( 'num_float' , Float (num_str)) # 10.6 # STR to list first_name = 'Asabeneh' print (first_name) # 'Asabeneh' first_name_to_list = list ( first_name ) print ( first_name_to_list ) # ['A', 's', 'a', 'b', 'e', ' n', 'e', 'h']Copy the code

Figure 2.4

Numeric data types in Python:

  1. Integer: integers (negative, zero, and positive) Example:… -3, -2, -1, 0, 1, 2, 3…
  2. Floating point (decimal number) Example:… -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5…
  3. Examples of complex numbers: 1 + j, 2 + 4J, 1-1j

For more on Python learning experiences, follow me and keep me updated.