WE CODE NOW
  • Home 
  • Blog 
  • Guides 
Guides
  1. Home
  2. Guides
  3. Python Programming
  4. Essential Python Syntax for Beginners

Essential Python Syntax for Beginners

Posted on June 1, 2024  (Last modified on June 8, 2024) • 2 min read • 362 words
Python
 
Beginners
 
Syntax
 
Python
 
Beginners
 
Syntax
 
Share via

Learn the fundamental Python syntax every beginner should know, including variables, data types, loops, and conditionals.

On this page
  • Variables and Data Types
    • Variables
    • Data Types
  • Loops and Conditionals
    • For Loop
    • While Loop
    • Conditionals
    • Nested Conditionals and Loops
  • Functions
  • Conclusion

Essential Python Syntax for Beginners  

Mastering Python starts with understanding its basic syntax. This guide will walk you through the core elements of Python syntax, including variables, data types, loops, and conditionals.

Variables and Data Types  

Variables  

Variables store information that can be referenced and manipulated in a program. Variables are created when you assign a value to them:

name = "Alice"
age = 30
is_student = True

Data Types  

Python has several built-in data types:

  • Integers: Whole numbers, positive or negative, without decimals (e.g., 1, -2, 100).
num = 10
  • Floats: Floating point numbers, or decimals (e.g., 1.5, -3.14).
price = 19.99
  • Strings: Sequence of characters enclosed in quotes (e.g., “hello”, ‘world’).
greeting = "Hello, World!"
  • Booleans: True or False values.
is_open = True
  • Lists: Ordered, mutable collections of items.
fruits = ["apple", "banana", "cherry"]
  • Tuples: Ordered, immutable collections of items.
coordinates = (10.0, 20.0)
  • Dictionaries: Unordered, mutable collections of key-value pairs.
person = {"name": "Alice", "age": 30}
  • Sets: Unordered collections of unique items.
unique_numbers = {1, 2, 3}

Loops and Conditionals  

For Loop  

Loop through a sequence of numbers using the range() function.

for i in range(5):
    print(i)

Loop through a list.

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

While Loop  

Execute a set of statements as long as a condition is true.

count = 0
while count < 5:
    print(count)
    count += 1

Conditionals  

Perform different actions based on different conditions using if, elif, and else.

if age > 18:
    print("Adult")
elif age == 18:
    print("Just turned adult")
else:
    print("Minor")

Nested Conditionals and Loops  

You can nest conditionals and loops within each other.

for i in range(3):
    if i % 2 == 0:
        print(f"{i} is even")
    else:
        print(f"{i} is odd")

Functions  

Functions are blocks of code that perform a specific task and can be reused.

def greet(name):
    return f"Hello, {name}!"

print(greet("Alice"))  # Output: Hello, Alice!

Functions can have default parameters.

def greet(name="World"):
    return f"Hello, {name}!"

print(greet())  # Output: Hello, World!
print(greet("Alice"))  # Output: Hello, Alice!

Conclusion  

These basics are essential building blocks for your Python journey. Practice these concepts to build a strong foundation in Python programming.

Advanced Python Functions and Lambdas 
On this page:
  • Variables and Data Types
    • Variables
    • Data Types
  • Loops and Conditionals
    • For Loop
    • While Loop
    • Conditionals
    • Nested Conditionals and Loops
  • Functions
  • Conclusion
Copyright © 2024 WE CODE NOW All rights reserved.
WE CODE NOW
Link copied to clipboard
WE CODE NOW
Code copied to clipboard