0% found this document useful (0 votes)
2 views

Undergraduate python class

The document provides an introduction to Python syntax, covering key concepts such as variables, data types, comments, and best practices for naming variables. It explains the use of indentation, quotation marks, and reserved keywords, as well as various data structures like lists, tuples, sets, and dictionaries. Additionally, it highlights methods associated with these data types and the importance of typecasting.

Uploaded by

Gyang Emmanuel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Undergraduate python class

The document provides an introduction to Python syntax, covering key concepts such as variables, data types, comments, and best practices for naming variables. It explains the use of indentation, quotation marks, and reserved keywords, as well as various data structures like lists, tuples, sets, and dictionaries. Additionally, it highlights methods associated with these data types and the importance of typecasting.

Uploaded by

Gyang Emmanuel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture Slide

Department of Chemical Engineering


Abubakar Tafawa Balewa University, Bauchi

Course Lecturer: Dr. A. Y. Abdulkarim


DChE, ATBU. Bauchi
Introduction to Python Syntax and
Indentation and Quotation Marks
Variables
•Indentation:
•Python Syntax:
• Used to define blocks of code (4 spaces).
•Rules for writing code understood by the
• Example:
Python interpreter.
•Variables:
•Containers for storing data. •Quotation Marks:
•Example: x = 2 •Single (') or double (") for strings.
•Variables are references to objects, not •Example: print("Hello world")
the objects themselves.
Comments and Docstrings Variables and Data Types
•Comments: •Variables:
•Start with # and are ignored by the •Assign values using =.
interpreter. •Example: x = 2, greeting = "hello world"
•Example: # This is a comment •Data Types:
•Docstrings: •Integers, floats, strings, Booleans, lists,
•Enclosed in triple quotes ("""), used for tuples, dictionaries, sets.
documentation. •Example: num = 2.0, name = "Lloyd"
•Example:
Python Reserved Keywords
Multiple Variables and Reassignment •Reserved Words:
•Multiple Variables: •Cannot be used as variable names.
•Assign multiple variables to the same or •Example: False, None, True, and, if, else, etc.
different values. •Check with: import keyword;
•Example: x = y = 23, a, b, c = 12, "Love", False print(keyword.kwlist)
•Reassignment:
•Variables can be reassigned. Best Practices for Variable Names
•Example: x = "old me", x = "new me" •Descriptive Names:
•Use lowercase and underscores for readability.
•Example: student_names = ["Mary", "Peter"]
Variable Naming Rules •Avoid Abbreviations:
•Allowed Names: •Keep names concise but descriptive.
•Start with letters or underscores, can contain
numbers. Numeric Data Types
•Example: num = 2, _name = "John" •Integers:
•Illegal Names: •Whole numbers (e.g., x = 5).
•Cannot start with numbers, dashes, or spaces. •Floats:
•Example: -name = "John" (Error) •Decimal numbers (e.g., x = 5.5).
•Complex Numbers:
•Real and imaginary parts (e.g., x = 5 + 2j).
Boolean and None Types Lists
•Boolean: •Lists:
•True or False. •Ordered, mutable, can contain duplicates.
•Example: x = 5 > 2 (returns True). •Example: age = [23, 34, 45, 56, 67, 78]
•NoneType: •Accessing Elements:
•Represents null or no value. •Use indexing (e.g., age[0]).
•Example: x = None List Methods
Strings •Common Methods:
•Strings: •append(), extend(), remove(), pop(), sort(),
•Sequence of characters, enclosed in quotes. reverse(), count(), index().
•Example: a = "Am learning Python" •Example: names = ["John",
•Accessing Characters: "Oscar"], names.append("Rose").
•Use indexing (e.g., x[0] for the first character). Tuples
•Tuples:
String Methods •Immutable, ordered sequences.
•Common Methods: •Example: animals = ("Lion", "Tiger", "Elephant")
•capitalize(), upper(), lower(), replace(), split(), st •Methods:
rip(), count(), index(). •count(), index().
•Example: str1 = "Am learning Python",
• str1.upper() returns "AM LEARNING PYTHON".
Sets
•Sets: •Typecasting:
•Unordered, mutable, no duplicates. •Converting one data type to another.
•Example: animals = {"Lion", "Tiger", "Elephant"} •Example: x = 2.5, x = int(x) converts x to an integer.
•Methods: •Functions: int(), str(), float(), bool().
•add(), remove(), pop(), union(), intersection().

Dictionaries
•Dictionaries:
•Key-value pairs, mutable.
•Example: dict1 = {"name": "Mary", "age": 22}
•Accessing Values:
•Use keys (e.g., dict1["name"] returns "Mary").

Dictionary Methods
•Common Methods:
•keys(), values(), items(), get(), pop(), update().
•Example: dict1.keys() returns ["name", "age"].

You might also like