今日已更新 287 条资讯 | 累计 20259 条内容
关于我们

Python for Beginners — Part 2: Variables, Data Types & Numbers

Ramesh S 2026年06月20日 11:29 4 次阅读 来源:Dev.to

Part 2 of a beginner-friendly series on learning Python from scratch. In Part 1 , we installed Python, wrote our first program, and learned the syntax rules that hold everything together. Now it's time to start storing and working with information — which means variables and data types. What is a Variable? A variable is a name that points to a value stored in memory. Think of it as a labeled container you can put something into, and refer back to later by name. name = " Ramesh " age = 25 Unlike many other languages, Python doesn't need you to declare a variable's type ahead of time. You just assign a value with = , and Python figures out the type on its own. This is called dynamic typing . x = 5 # x is an integer x = " hello " # now x is a string — totally legal in Python This flexibility is convenient, but it also means you need to be a little more careful — Python won't stop you from changing a variable's type halfway through your program, even if that wasn't your intention. Variable Naming Rules Python is strict about how variable names can look: Must start with a letter or an underscore ( _ ) — never a number. Can only contain letters, numbers, and underscores. Cannot be a Python keyword ( class , for , if , etc.). Are case-sensitive — age , Age , and AGE are three different variables. age = 25 # valid _age = 25 # valid age2 = 25 # valid 2 age = 25 # invalid — cannot start with a number my - age = 25 # invalid — hyphens aren't allowed Naming conventions Python's style guide (PEP 8) recommends snake_case for variable names — lowercase words separated by underscores: first_name = " Ramesh " total_score = 95 Assigning Multiple Variables Python lets you assign several variables in a single line, which keeps code compact and readable. # One value to multiple variables x = y = z = 10 # Multiple values to multiple variables name , age , city = " Ramesh " , 25 , " Chennai " Data Types in Python Every value in Python belongs to a data type, which determines what kind of

本文内容来源于互联网,版权归原作者所有
查看原文